Skip to content

1.2 Basic syntax and concepts

Let's get an overview of Python's basic syntax and main concepts that we will be covering during this course.

1.2a Python syntax

Throughout the whole course, we will be focusing on various Python syntax . However we feel that the following three areas should be stated immediately since they're very useful.

  • Case sensitivity - Python is case-sensitive, meaning Variable and variable are different identifiers.

  • Indentation - In Python, indentation is used to group statements together to form a block of code. This is explained in detail here.

  • Comments - These are explained proper detail here.

    • Single-line comments start with a #.
    • Multi-line comments can be written with triple quotes (''' or """).

Example 1.2.1 - Single line and multi-line comments

# This is a single-line comment

'''
This is a 
multi-line comment
'''

"""
This is also a
multi-line comment
"""
 


 

1.2b Variables and data types

  • Variables are used to store data values.

  • Variable names must start with a letter or underscore, followed by letters, numbers, or underscores.

These will be covered in Topic 2 - Variables & Input.

Example 1.2.2 - Variables

score = 5
name = "Thea"
is_student = True

 


 

1.2c Output

Then we move on to the output topic and we use the classic simple 'Hello World!' example.

Example 1.2.3 - Basic print statement

print("Hello, World!")
We shall explore various features of the print() function and the useful features of the F-Strings.

 


 

1.2d Operators

Arithmetic operators are symbols in programming languages that perform mathematical operations on numbers. Hence we will understand how to use basic arithmetic operations like:

  • + (addition)
  • - (subtraction)
  • * (multiplication)
  • / (division)
  • % (modulus)
  • ** (exponentiation)
  • // (floor division)

Example 1.2.4 - + (addition) operator

3 + 2
 

In this topic, we will also understand how to use assignment operator to assign values to variables.

Example 1.2.5 - += (add and assign) operator

x += 3

 


 

1.2e Decisions (conditional) statements

In topic 5, we shall understand the conditional statements with if, elif and else. We will also explore the nested decision statements that are just statements placed inside another decisions statements.

We will use the following comparison operators to practice decision examples:

  • == (equal)
  • != (not equal)
  • > (greater than)
  • < (less than)
  • >= (greater than or equal to)
  • <= (less than or equal to)

We will also practice with the following logical operators to practice more decision examples:

  • and
  • or
  • not

Example 1.2.6 - Conditional operator !=

input != 0

 


 

1.2f Iterations (loops)

Python loops are constructs that allow you to execute a block of code repeatedly, based on a condition or a sequence. We need to understand the need and use of the for-in and while loops. Here we will explore that while loop can be paired with an else block. Nested loops will also be explored here, and we will be exploring the loop control statements - break, continue and pass. These will be covered here.

Example 1.2.7 - Basic for-in loop

for i in range(5):
    print(i)

 


 

1.2g Functions and modules

A Python function is a block of reusable code designed to perform a specific task whereas a Python module is a file containing Python definitions and statements.

In topic 7, we will be explaining the distinction between Python functions and modules, highlighting how they serve different purposes in code organization and reuse.

 


 

1.2h Data structures

Python provides several built-in data structures to help organize and manage data efficiently. In this topic, we will be explaining data structures, which are organized ways to store, manage, and manipulate data efficiently. Specifically, we will tackle lists, tuples, and dictionaries, exploring their unique characteristics and use cases.

Example 1.2.8 - Python list

fruits = ['apple', 'banana', 'cherry']

 


 

1.2i Graphical interface

In this particular topic, we will be explaining how to develop a program that includes a graphical interface using the Turtle module and/or the Tkinter module. These modules allow you to create visual elements and interactive components in your Python programs, enabling you to design more engaging and user-friendly applications. By the end of this topic, you will have the skills to build and customize graphical interfaces, enhancing the overall functionality and appeal of your projects.

 


 

1.2j Good programming practices

Good programming practices are important because they improve code readability and maintainability and facilitate debugging and troubleshooting. In the last topic, we will be explaining good programming practices, including the use of inline comments, block comments, multi-line strings or docstrings, code indentation, and meaningful variable names. These practices are crucial for writing clear, maintainable, and readable code. Proper code indentation ensures that the structure of the code is visually apparent, and meaningful variable names make the code self-explanatory. Adhering to these practices enhances collaboration, debugging, and future modifications.

 


 

Note

Python's syntax is designed to be clean and readable, making it an excellent language for beginners. Understanding these basic concepts and syntax will give you a solid foundation to start coding in Python and progress to more advanced topics.