First look ========== An **important** part of learning any programming languge is **to be able to read** it. At the beginning, looking at a piece of code will be mostly overwhelming. The following piece of code calculates if a number is a `prime number `_ or not (right now it does not matter at all if You don't understand how it works): Elements of Python 3 -------------------- .. interactive_code_block:: :caption: Some example code import math def is_prime_number(whole_number): is_prime_number = whole_number > 1 start_value = 2 stop_value = int(math.sqrt(whole_number)) for i in range(start_value, stop_value + 1): if whole_number % i == 0: is_prime_number = False break return is_prime_number whole_number = 4441 # Change the value of this number print('{} is{} a prime number'.format(whole_number, [' not', ''][is_prime_number(whole_number)])) ► Click on 'Run' to run the code and see its output in the area below the buttons. If You want to test a different number, You can change the value ``4441`` in line 13 to any other positive `whole number `_ (= no decimals like e.g. 4441.5). Though only 14 lines long, this code includes a lot of different elements of the Python programming language. Those elements can be split into 2 groups: * **Pre-defined elements**, which are built into the programming language and must be written *exactly* the way they are defined (Python is *case-sensitive*, thus ``true`` is not the same as ``True``): * Keywords (also known as reserved words): ``import``, ``def``, ``True``, ``for``, ``in``, ``if``, ``break``, ``return`` * Built-in functions: ``int()``, ``range()``, ``print()``, ``format()`` * Built in data types: ``str`` (the text in single-quotes ``'``), ``int`` (the numbers) * A module from the built in library: ``math`` * Delimiters: ``(``, ``)``, ``:``, ``=``, ``,``, ``.``, ``[``, ``]``, ``'`` * Operators: ``+``, ``%``, ``==`` * Syntax: the 'grammar' of the programming language * **Elements defined by the programmer**, which can be written the way how they suit the purpose of the program (following a few rules): * Identifiers: ``whole_number``, ``is_prime_number``, ``start_value``, ``stop_value``, ``i`` * Literals: ``4441``, ``'{} is{} a prime number'``, ``' not'`` * A comment: ``# Change the value of this number`` * A function definition: ``def is_prime_number(whole_number):`` * A for-loop: ``for i in range(start_value, stop_value + 1):`` * A condition: ``if whole_number % i == 0:`` * An expression within the condition: ``whole_number % i == 0`` Hello, World! ------------- Let's start with a much simpler (and tradional) program. .. interactive_code_block:: :caption: Hello, World! print('Hello, World!') Its code consists of one *statement* using the *built-in function* ``print()`` to output the *string literal* ``'Hello, World!'``. String is the programmers word for text (think string = chain of characters) and literal means that it is directly written in the code. Notice that the text is delimited by single-quotes ``'``. **Since we are going to use the built-in print() function quite often, let's look at a some details.** There is some freedom here to add extra spaces or round brackets ``()``, but that should only be used if it improves *readability*. .. interactive_code_block:: :caption: Variations that produce exactly the same output # Double quotes instead of single quotes print("Hello, World!") # Spaces between function name and first bracket print ('Hello, World!') # Spaces between string literal and brackets print( 'Hello, World!' ) # Extra pair of round brackets print( ('Hello, World!') ) # Multiple string literals separated by comma(s) print('Hello,', 'World!') # Statement split over two lines print('Hello, \\ World!') # Triple-quoted string = 'docstring', mostly used for documentation utilities print("""Hello, World!""") The text after the hash character ``#`` until the end of the line is a *comment* and will be ignored. Comments are used to add information e.g. about the purpose of the code that cannot easily be concluded by reading the statements. .. interactive_code_block:: :caption: Variations that throw an error :^( # Space at the beginning of the statement print('Hello, World!') # Quotes missing print(Hello, World!) # Double-quotes and single-quotes mixed print('Hello, World!") # Backslash missing in multi-line statement print('Hello, World!') Each of the statements above throw an error (and stop the program). |br| ► Try to correct all of them, one statment after the other, and watch the error messages. As You can see, the automatic coloring of the code is failing (although not completely), which helps to spot syntax errors. .. interactive_code_block:: :caption: Variations that showcase some string trickery # Double-quotes can be use in a single-quote delimited string print('Hello, "World!"') # ..or the other way around print("Hello, 'World!'") # Same-style quotes can be added if they are 'escaped' = preceded by a backslash print('Hello, \\'World!\\'') # There are a few other escape characters You can use like \\t = tab-character print('\\tHello, World!') # or \\n = new line print('Hello,\\nWorld!') # New lines, single-quotes, and double quotes can be used directly within docstrings print(""""Hello", 'World!'""") It's `recommended `_ to pick one quote variant (single or double) and use it consistently. Switching to the other variants is ok if it improves the readability by e.g. avoiding `escape characters `_.