Author: Siddharth Pampana | Last updated: 2025-02-24
Variables, Expressions, and Statements in Python
What You'll Learn and Why It's Important
In this lesson, you will explore the fundamental building blocks of Python programming. These concepts are critical for creating any type of Python program, and by mastering them, you’ll be well on your way to understanding more complex topics.
You'll learn how to:
- Identify different types of data in Python.
- Use variables to store and manipulate information.
- Perform calculations using operators and operands.
- Write conditional and looping statements to control program flow.
2.1 Values and Types
In Python, every value has a type. Understanding the different types of data will help you work with values more effectively.
Common types include:
- Integer: Whole numbers like
42
or-7
. - Float: Numbers with decimal points like
3.14
or-0.001
. - String: Text values enclosed in quotes, such as
"Hello, World!"
.
Here’s how you can check the type of any value:
Type Checking Example
It's important to know the type of a value because it affects how the value behaves. For example, you can perform arithmetic on integers and floats, but not on strings.
2.2 Variables
A variable is a way to store a value so that you can use it later in your program. Think of a variable as a labeled box that holds data.
Variable Example
Variables allow you to store data and reuse it in different parts of your program. They also make your code more flexible because you can easily change the value of a variable without having to rewrite the code.
2.3 Variable Names and Keywords
When naming variables, you must follow certain rules:
- A variable name must start with a letter or an underscore.
- It can contain letters, numbers, and underscores.
- Variable names are case-sensitive (
myVar
is different frommyvar
).
You cannot use reserved Python keywords (such as if
, else
, or for
) as variable names.
Valid and Invalid Variable Names
Choosing good variable names helps make your code more readable and understandable. Avoid using single-letter names unless it’s clear what they represent.
2.4 Statements
A statement is an instruction that Python can execute. We've already seen assignment statements, where you assign a value to a variable. There are also other types of statements, like control flow statements (if
statements) and loop statements (for
and while
loops).
Statement Types
Statements are the core of Python programs, telling the interpreter what to do. By combining different types of statements, you can build powerful, flexible programs.
2.5 Operators and Operands
Operators are symbols that perform operations on variables and values. The values that the operators act on are called operands.
Types of operators:
- Arithmetic Operators: Perform basic math operations (
+
,-
,*
,/
,//
,%
,**
). - Comparison Operators: Compare two values (
==
,!=
,>
,<
,>=
,<=
). - Logical Operators: Combine multiple conditions (
and
,or
,not
).
Arithmetic Operators
Operators allow you to manipulate values and variables in your programs. Understanding how to use them will help you perform calculations, compare values, and make decisions in your code.
2.6 Expressions
An expression is any combination of values, variables, and operators that produces a new value. In Python, an expression can be as simple as a number or as complex as a series of operations.
Expression Example
Expressions are evaluated by Python to produce a result. You can use expressions in calculations, comparisons, and other operations. Every time an expression is evaluated, it returns a value.
2.7 Order of Operations
Python follows the same order of operations as mathematics, often referred to as PEMDAS:
- Parentheses
- Exponents
- Multiplication and Division
- Addition and Subtraction
Order of Operations
Order of operations is crucial when writing expressions in Python. Using parentheses helps clarify the order in which operations should be performed, ensuring your code produces the correct results.
2.8 Modulus Operator
The modulus operator (%
) returns the remainder of a division. This operator is useful when you need to check whether a number is even or odd, or when you want to wrap numbers around a fixed range (such as in circular data structures).
Modulus Operator
The modulus operator is often used in programming to check for divisibility, find remainders, and handle cyclic structures (such as wrapping around the end of a list).
2.9 String Operations
Strings in Python can be combined or manipulated using various operators:
- Use
+
to concatenate (join) strings. - Use
*
to repeat strings a specified number of times.
String Operations
String operations are useful for formatting text, building dynamic messages, and creating repeated patterns in your programs. Strings can be combined in many ways to produce more complex outputs.
2.10 Asking the User for Input
Python allows you to gather input from the user with the input()
function. The input is always returned as a string, so you may need to convert it to other types, such as an integer, using the int()
function.
User Input Example
Handling user input is essential in many programs, especially those that interact with users. Be sure to handle different data types appropriately and provide clear instructions to the user.
2.11 Comments
Comments are notes you add to your code to explain what it does. Python ignores comments during execution, so they don't affect the program's behavior.
Comment Example
Comments are helpful for explaining your code to others (or yourself) when you return to it later. Always use comments to clarify complex parts of your code or to provide context for why something is done a certain way.
2.12 Choosing Mnemonic Variable Names
Choosing meaningful names for your variables makes your code easier to read and maintain. Always use names that describe the purpose of the variable.
Good Variable Naming
Good variable names reduce confusion and make your programs more readable. Choose names that convey meaning and help others (or yourself) understand the code's intent.
2.13 Debugging
Debugging is the process of finding and fixing errors in your code. Strategies include:
- Print Statements: Use
print()
to see the values of variables. - Error Messages: Read the error messages Python provides; they often tell you what's wrong and where.
- Break Down the Problem: Isolate the issue by simplifying the code.
2.14 Glossary
- Name
Variable
- Type
- Description
A named container that holds data and can be used and modified during program execution.
- Name
Expression
- Type
- Description
A combination of values, variables, and operators that is evaluated to produce a value.
- Name
Operator
- Type
- Description
A symbol that performs an action on one or more operands (e.g.,
+
,-
,*
,%
).
- Name
Modulus Operator
- Type
- Description
An operator (
%
) that returns the remainder of a division.
- Name
Comment
- Type
- Description
A note in the code ignored by Python but useful for developers.
2.15 Exercises
- Exercise 1: Create a variable
my_age
and assign your age to it. Then print "I am X years old" using the variable.
Exercise 1
Understanding how to assign and print variables will help you in creating more complex programs that involve user interaction and output.
- Exercise 2: Write a program that asks the user for two numbers and prints their sum.
Exercise 2
Adding numbers is one of the simplest mathematical operations in programming. This exercise helps solidify your understanding of handling user input and performing calculations.
- Exercise 3: Write a program that checks whether a number is even or odd using the modulus operator.
Exercise 3
The modulus operator is a powerful tool for checking divisibility. This exercise demonstrates how to use it in a practical context.
- Exercise 4: Use comments to explain the following code:
Exercise 4
Adding comments to your code helps both you and others understand what it’s doing. This exercise emphasizes the importance of clear and concise comments.