Python - Rules of the Game

Please refer to this tutorial to find more details on the following codes.


ConceptKeywordsDescriptionCode Examples
1 Printing textprint Keyword 'print' is used to print information. If you add comma after a print statement,

the output of the following print statement is shown in the same line.

Example1, Example2, Example3, Example4,
2 Comments You can add comments in your code by using the symbol '#'. All text right of '#' are ignored by the program.Example1, Example2
3 Calculations with numbers Standard arithmetic symbols like '+','-','*','/' work the same way in Python.

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

The symbol '%' gives the modulo after division.

Floating point numbers can be declared by using a decimal point.

Example1
4 Numbers and Boolean Variablesand, or, not Variables store numbers or text for later use in the program. Logical variables hold values True/False. They are useful in controlling the flow of the program.Example1, Example2, Example3, Example4, Example5, Example6
5 String Variables String variables store characters.Example1, Example2, Example3
6 Python is not Algebra The meanings of instructions like x=x+5 is different between Python and algebra.Example1, Example2, Example3, Example4
7 Lists and Dictionariesin, del Lists and dictionaries hold multiple numbers together. The indices of lists are ordered integers starting from 0, whereas dictionaries can have string-based indices.Example1, Example2, Example3
8 Keywords Are Special Words Python's 31 keyworlds are special. You cannot name a variable using any of them.Example1
9 Built-in Functions Python has many useful built-in functions to simplify tasks - e.g. -range(), len(), sort(), float(), int(), str().Example1
10 List-related Functions Python includes a number of useful functions for processing lists, e.g. index(), append(), remove(), del().Example1
11 String-related Functions Python string acts like an immutable list, and all list-related functions are also ported to strings. Additional string specific functions - toupper(), tolower(), strip().Example1
12 Standard Flow of Code 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.
13 Conditional Statementsif, else, elif The keywords 'if', 'else' and 'elif' are used for conditional execution of certain lines of code.Example1, Example2
14 'for' Loopsfor The keyword 'for' is used to loop over the same code many times.Example1, Example2, Example3
15 'while' Loopswhile, break, continue The keyword 'while' is used to loop over the same code until a condition is satisfied.Example1, Example2, Example3, Example4
16 Input/Output The functions input() and raw_input() process input from the terminal. The functions readline() and readlines() read lines from a given file.You cannot run this in the sandbox.
17 Multi-file Codeimport You cannot run this in the sandbox.
18 Creating New Functionsdef, return A function means lines of code with a given name. When the name is called later in the program, the corresponding lines of code are executed. Apart from in-built functions, users can create own functions using keywords 'def' and 'return'.Example1, Example2, Example3, Example4
19 Classesclass A class combines variables and functions together in one 'object'.Example1
20 Iterables Example1
21 Library - Regular Expression Regular expression is a special sublanguage to make searches through strings easy. Python's 're' library facilitates the use of regular expressions.Example1, Example2, Example3, Example4
22 Library - Random the functions randint() and uniform() from 'random' library are used to generate random numbers.Example1, Example2