Python - Rules of the Game

A python script starts from the first line of code in the file and runs every line until finishing with the last line of code in the file.


CategoryKeywordDescriptionCode examples
1 Code - printing textprint Keyword 'print' is used to print information. This command adds a newline after the output.

You can add comments in your code by using the symbol '#'. All text right of '#' are ignored by the program.

Example1, Example2
2 Calculations with numbersfloat Standard arithmetic symbols like '+','-','*','/' work in python.

The symbol '**' gives power of a number.

The symbol '%' gives the modulo after division.

Floating point numbers can be declared by using 'float' or using a decimal point.

Example1, Example2,

Example3, Example4

3 Data type - string Strings in pythons can be defined like any variable. The symbol '+' is used for string catenation.Example
4 Data type - sequence of numbersrange The keyword 'range' is used to specify a set of integers.Example1, Example2,

Example3, Example4

5 Data type - listlen Lists (arrays) contain a collection of numbers or other elements.Example
6 Data type - dictionary Dictionaries (associative arrays) are like arrays, but their indices can be strings instead of ordered integers 0, 1, 2, 3 etc.Example
7 Code - loopfor, in The keyword 'for' is used to loop over the same code many times.Example1, Example2, Example3
8 Code - loopwhile, break, continue The keyword 'while' is used to loop over the same code until a condition is satisfied.Example1, Example2, Example3
9 Code - conditionif, else, elif The keywords 'if', 'else' and 'elif' are used for conditions.Example
10 Code - functiondef A function means lines of code that is given a name. When the name is called later in the program, the corresponding lines of code are executed.Example