Expressions and statements

Expressions and statements

Python, like any programming language, operates on a fundamental structure of expressions and statements. Understanding these concepts is crucial for writing effective code.

Expressions

In essence, an expression is a combination of values, variables, operators, and function calls that can be evaluated to produce a result. Think of them as the “calculations” your code performs.

Arithmetic Expressions

Arithmetic expressions involve mathematical operations like addition, subtraction, multiplication, division, and modulo (remainder).

x = 10 + 5  # Addition

y = 8 * 3   # Multiplication

z = 20 / 4  # Division

result = 17 % 3  # Modulo (result is 2)

In the first line, the values 10 and 5 are added together, and the result is assigned to the variable x.

The second line multiplies 8 by 3, storing the outcome in y.

Division (/) yields a floating-point result in Python 3.

The modulo operator (%) calculates the remainder after division.

Boolean Expressions

Boolean expressions evaluate to either True or False. They use comparison operators (==, !=, >, <, >=, <=) and logical operators (and, or, not).

age = 25

is_student = True

is_adult = age >= 18  # True

is_not_student = not is_student  # False

The first line assigns the value 25 to the variable age.

The second line assigns the boolean value True to the variable is_student.

The third line uses the comparison operator >= to check if age is greater than or equal to 18.

The result, True, is assigned to the variable is_adult.

The fourth line uses the logical operator not to negate the value of is_student. Since is_student is True, not is_student evaluates to False, and this value is assigned to the variable is_not_student.

Statements

Statements are the instructions that tell Python to do something. They can assign values, control the flow of your program, define functions, and more.

print() Statement

The print() statement is your primary tool for displaying results to the user.

message = “Hello, world!”

print(message)  # Output: Hello, world!

The first line assigns the string value “Hello, world!” to the variable message. The second line uses the print() function to display the value of message to the user. The output will be the string “Hello, world!”.

end Parameter in print()

By default, print() adds a new line after the output. You can change this behavior using the end parameter.

print(“Hello,”, end=” “)

print(“world!”)  # Output: Hello, world!

The first print() statement will print “Hello,” followed by a space instead of a newline, because of the end=” “ parameter.

The second print() statement will print “world!” followed by a newline. The combined output is “Hello, world!” on the same line.

Newline Character (\n)

You can manually insert newlines within strings using the \n character.

print(“Line 1\nLine 2”)

Output:

Line 1

Line 2

Raw Strings (r””)

Raw strings (prefixed with r) treat backslashes (\) as literal characters, which is useful for working with regular expressions and file paths.

path = r”C:\Users\Documents\file.txt”

print(path)  # Output: C:\Users\Documents\file.txt (no special interpretation of \)

input() Statement

The input() statement allows your program to get information from the user.

name = input(“Enter your name: “)

print(“Hello,”, name) 

The first line will prompt the user to enter their name. Whatever they enter will be stored as a string in the variable name.

The second line will then print “Hello,” followed by the value of the variable name, which is the name the user entered.

Typecasting User Input

User input is always received as a string. If you need to work with numbers, you’ll need to convert the input using functions like int() (for integers) or float() (for floating-point numbers).

age_str = input(“Enter your age: “)

age = int(age_str)  # Convert to integer