Category Archives: Library

4.1 Variable Names

Variable Names A variable can have a short name (like x and y) or a more descriptive name (age, car name, total_volume). Rules for Python variables: A variable name must start with a letter or the underscore character A variable name cannot start with a number A variable name can only contain alpha-numeric characters and… Read More »

4. Variables

Variables are containers for storing data values. Creating Variables Python has no command for declaring a variable. A variable is created the moment you first assign a value to it. Example x = 5y = “John”print(x)print(y) Output: 5John Variables do not need to be declared with any particular type, and can even change type after they have been… Read More »

3. Comments

Comments can be used to explain Python code. Comments can be used to make the code more readable. Comments can be used to prevent execution when testing code. Creating a Comment Comments starts with a #, and Python will ignore them: Example #This is a commentprint(“Hello, World!”) Comments can be placed at the end of a… Read More »

2. Syntax

Execute Python Syntax As we learned in the previous page, Python syntax can be executed by writing directly in the Command Line: >>> print(“Hello, World!”) Output:Hello, World! Or by creating a python file on the server, using the .py file extension, and running it in the Command Line: C:\Users\Your Name>python myfile.py Python Indentation Indentation refers… Read More »

1. Introduction

What is Python? Python is a popular programming language. It was created by Guido van Rossum, and released in 1991. It is used for: web development (server-side), software development, mathematics, system scripting. What can Python do? Python can be used on a server to create web applications. Python can be used alongside software to create… Read More »

Python

Introduction Python Installation Syntax Comments Variables Variable Names Assign Multiple Values Output Variables Global Variables Data Types Numbers Casting Strings Slicing Strings Modify Strings String Concatenation Format-Strings Escape Characters String Methods Booleans Operators Lists Access List Items Change List Items Add List Items Remove List Items Loop Lists List Comprehension Sort Lists Copy Lists Join… Read More »

59. Error handling during file operations

Sometimes during file operations, errors may also creep in. For example, a file being opened for reading might not exist. Or a file name used for a new file may already exist. Or an attempt could be made to read past the end-of-file. Or such as invalid operation may be performed. There might not be… Read More »

57. File Stream

So far, we have been using the iostream standard library, which provides cin and cout methods for reading from standard input and writing to standard output respectively. This tutorial will teach you how to read and write from a file. This requires another standard C++ library called fstream, which defines three new data types − Sr.No Data Type & Description 1 ofstream… Read More »

58. Reading and writing to files

Writing to a File While doing C++ programming, you write information to a file from your program using the stream insertion operator (<<) just as you use that operator to output information to the screen. The only difference is that you use an ofstream or fstream object instead of the cout object. Reading from a File You read information from a… Read More »

56. Custom Exception class

User-Defined Exceptions The new exception can be defined by overriding and inheriting exception class functionality. Let’s see the simple example of user-defined exception in which std::exception class is used to define the exception. #include <iostream>  #include <exception>  using namespace std;  class MyException : public exception{      public:          const char * what() const throw()          {              return “Attempted to divide by zero!\n”;          }  };  int main()  {      try      {          int x, y;          cout << “Enter the two numbers : \n”;          cin >> x >> y;          if (y == 0)          {              MyException z;              throw z;          }          else          {              cout << “x / y = ” << x/y << endl;          }      }      catch(exception& e)      {          cout << e.what();      }  }   Output: Enter the two numbers : 10 2 x / y = 5 Output: Enter the two numbers : 10 0 Attempted to divide by… Read More »