Institute of Computer Science
  1. Courses
  2. 2025/26 fall
  3. Introduction to Programming (MTAT.03.236)
ET
Log in

Introduction to Programming 2025/26 fall

  • Home Page
  • Introduction
  • Expressions
  • Conditional Execution
  • Functions
  • Iterations
  • Strings
  • Files
  • Lists
  • Graphics?
< eelmineTable of Contentsnext >

2.1 SYNTAX FUNDAMENTALS

Syntax specifies the rules and structure of how a program should be correctly organized in a particular programming language. Each programming language has unique syntax rules you need to follow. This chapter will give you an overview of Python syntax’s fundamental characteristics.

COMMENTS

Comments are used to explain how a particular piece of code works, making it more readable and understandable. They are ignored by the Python interpreter and do not impact the program's execution. Comments are valuable and serve as a helpful guide for yourself and others who will interact with a codebase in the future. However, make sure that the comments are succinct and not overwhelming to digest.

INDENTATION

Python relies on indentation to distinguish blocks of code. Indentation refers to the use of whitespace before code. If the lines are the same distance from the left, they belong to the same code block. Indentation must be consistent across a code block. Notice how the if-else block is indented compared to the for-loop and how the print statements are indented within the if-else block. Don’t feel overwhelmed by the example; you’ll learn about conditional statements and loops in the upcoming weeks.


for i in range(3):
    if i == 2:
        print("i is equal to two")
    else:
        print("i is not equal to two")

WHITESPACE

Note that the whitespace between lines or even within lines does not matter. The primary consideration related to whitespace is readability. It helps ensure the program is easy to navigate and understand for yourself and others who might interact with the source code.


# These expressions are equivalent
a = 2 + 7

b = 8+9

# We can remove the whitespace between the two expressions too
a = 10 + 5
b = 10+5

PRINT FUNCTION

The print() function in Python is one of the most commonly used functions built into Python. It is used to print out content, such as sentences, numbers, and more. Notice that print requires parentheses () after it.


print("Hello World!")
# This prints out "Hello World" in the console

RESERVED KEYWORDS

Reserved keywords are words that are part of the language's syntax and have special meanings. Python reserves these words because they serve a purpose within the programming language and, hence, cannot be used as identifiers for variables, functions, or any other user-defined names. Using these keywords as identifiers would result in a syntax error as shown below.

The following keywords are reserved per the latest version of Python 3.12.1.


False      await      else       import     pass
None       break      except     in         raise
True       class      finally    is         return
and        continue   for        lambda     try
as         def        from       nonlocal   while
assert     del        global     not        with
async      elif       if         or         yield

Here's screenshot of Thonny showing an example of what happens when trying to use a reserved keyword as a variable name.

KEY THINGS TO REMEMBER

  • Comments Use # for single-line comments and triple quotes ''' for multi-line comments.
  • Indentation refers to whitespace before statements and indicates a specific code block. Be careful, as wrong indentation will cause errors.
  • Whitespace within and between lines does not alter how the program works but is still important for maintaining readability.
  • print() is a built-in function used to output information.
  • Reserved keywords are special words that cannot be used as user-defined identifiers.

SELF-CONTROL EXERCISE

< eelmineTable of Contentsnext >

  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment