Skip to content
Home / Fundamentals

Python Statement

In Python, a statement is an instruction that the interpreter can execute. Each statement must be written on a separate line and must end with a newline character (\n) or a semicolon (;).

Here are a few examples of Python statements:

x = 10  # Assignment statement
print("Hello, World!")  # Function call statement
if x > 5:  # Conditional statement
    print("x is greater than 5")  # Indented statement
for i in range(10):  # Loop statement
    print(i)  # Indented statement

In the first example, x = 10 is an assignment statement that assigns the value 10 to the variable x.

In the second example, print("Hello, World!") is a function call statement that calls the print() function and passes it the string "Hello, World!" as an argument.

In the third example, if x > 5: is a conditional statement that specifies that the indented statements following it (print("x is greater than 5")) should be executed only if x > 5 is True.

In the fourth example, for i in range(10): is a loop statement that specifies that the indented statements following it (print(i)) should be executed 10 times, with the variable i taking on the values 0 through 9.

Python Indentation

In Python, indentation is used to indicate blocks of code. In other programming languages, blocks of code are often delimited by curly braces ({ and }). In Python, however, indentation is used to indicate blocks of code.

For example, consider the following code:

x = 10
if x > 5:
    print("x is greater than 5")
    print("This line is indented, so it is part of the if block")
print("This line is not indented, so it is not part of the if block")

In this example, the lines print("x is greater than 5") and print("This line is indented, so it is part of the if block") are indented. This indicates that they are part of the block of code that is executed if x > 5 is True. The line print("This line is not indented, so it is not part of the if block") is not indented, so it is not part of the if block.

It is important to use consistent indentation in your code. In Python, it is conventional to use four spaces for each level of indentation. However, you can use any number of spaces as long as you are consistent within a single block of code.

# For loops
for i in range(10):
    print(i)
    print("This line is indented, so it is part of the for loop")

# While loops
i = 0
while i < 10:
    print(i)
    i += 1
    print("This line is indented, so it is part of the while loop")

# Functions
def greet(name):
    print("Hello, " + name)
    print("This line is indented, so it is part of the greet() function")

# Classes
class Dog:
    def __init__(self, name):
        self.name = name
        print("This line is indented, so it is part of the __init__() method")

In each of these examples, the indented lines of code are part of the block of code associated with the loop, function, or class.

It's also worth noting that indentation is important for indicating the end of a block of code. For example:

if x > 5:
    print("x is greater than 5")
print("This line is not indented, so it is not part of the if block")

In this case, the if block ends with the unindented line print("This line is not indented, so it is not part of the if block"), indicating that it is not part of the if block.

Python Variables

In Python, a variable is a name that refers to a value stored in memory. Variables are used to store values that can be used and manipulated by the program.

To create a new variable in Python, you can use the assignment operator (=). For example:

x = 10
y = "Hello, World!"
z = [1, 2, 3]

In Python, a variable is a name that refers to a value stored in memory. Variables are used to store values that can be used and manipulated by the program.

To create a new variable in Python, you can use the assignment operator (=). For example:

Copy code x = 10 y = "Hello, World!" z = [1, 2, 3] In this example, x is assigned the value 10, y is assigned the value "Hello, World!", and z is assigned the value [1, 2, 3].

It's important to note that Python is a dynamically-typed language, which means that the type of a variable is determined at runtime based on the value it is assigned. For example, x is an integer (int) because it is assigned the value 10, y is a string (str) because it is assigned the value "Hello, World!", and z is a list (list) because it is assigned the value [1, 2, 3].

You can use variables in your code just like you would use their values. For example:

x = 10
y = 20
z = x + y  # z is assigned the value 30
print(z)  # prints 30

Python Operators

Arithmetic operators: These operators perform basic arithmetic operations, such as addition (+), subtraction (-), multiplication (*), and division (/).

Comparison operators: These operators compare two values and return a Boolean value indicating whether the comparison is True or False. Examples include == (equal to), != (not equal to), > (greater than), < (less than), >= (greater than or equal to), and <= (less than or equal to).

Logical operators: These operators perform logical operations, such as and, or, and not. They are often used in combination with comparison operators to create more complex conditions.

Assignment operators: These operators assign a value to a variable. The most basic assignment operator is =, which assigns a value to a variable. There are also compound assignment operators that perform an operation and assign the result to a variable in a single step. Examples include +=, -=, *=, and /=.

Membership operators: These operators test whether a value is a member of a sequence (such as a list or a string). The in operator returns True if the value is a member of the sequence, and the not in operator returns True if the value is not a member of the sequence.

Identity operators: These operators test whether two values are the same object. The is operator returns True if the two values are the same object, and the is not operator returns True if they are not the same object.