Introduction
Welcome to the basics of Python! We all need to start somewhere, and this lab is the perfect place to do it. Python is an easy-to-learn, powerful programming language known for its elegant syntax and dynamic typing, making it an ideal language for scripting and rapid application development.
In this lab, you will learn about some of the most fundamental concepts in Python. We will cover:
- Mathematical operators and their order of precedence.
- Augmented assignment operators for more concise code.
- The Walrus Operator (
:=) for assigning variables within expressions. - Python's core data types.
- Basic string operations like concatenation and replication.
Let's get started!
Math Operators
In this step, you will learn about Python's mathematical operators and their precedence. Operator precedence determines the order in which operations are performed in an expression.
Here are the common math operators, from highest to lowest precedence:
| Operators | Operation | Example |
|---|---|---|
** |
Exponent | 2 ** 3 = 8 |
% |
Modulus/Remainder | 22 % 8 = 6 |
// |
Integer division | 22 // 8 = 2 |
/ |
Division | 22 / 8 = 2.75 |
* |
Multiplication | 3 * 3 = 9 |
- |
Subtraction | 5 - 2 = 3 |
+ |
Addition | 2 + 2 = 4 |
Note: If you are not familiar with using WebIDE (VS Code like), please learn it first at this lab.
Let's practice using these. Open the main.py file from the file explorer on the left. Clear any existing content and add the following code. We will use the print() function to see the result of each expression.
## Multiplication has higher precedence than addition
## So this evaluates as: 2 + (3 * 6) = 2 + 18 = 20
print(2 + 3 * 6)
## Parentheses override operator precedence
## This evaluates as: 5 * 6 = 30
print((2 + 3) * 6)
## Exponentiation
print(2 ** 8)
## Integer division
print(23 // 7)
## Modulus/Remainder
print(23 % 7)
## A more complex expression
print((5 - 1) * ((7 + 1) / (3 - 1)))
Now, run the script by opening a terminal and executing the following command:
python3 main.py
You should see the following output, with each result on a new line:
20
30
256
3
2
16.0
Augmented Assignment Operators
In this step, you'll explore augmented assignment operators. These provide a concise way to perform an operation on a variable and assign the new value back to it.
Here is a table of these operators and their equivalents:
| Operator | Equivalent |
|---|---|
var += 1 |
var = var + 1 |
var -= 1 |
var = var - 1 |
var *= 1 |
var = var * 1 |
var /= 1 |
var = var / 1 |
var //= 1 |
var = var // 1 |
var %= 1 |
var = var % 1 |
var **= 1 |
var = var ** 1 |
Let's practice using them. Clear the main.py file and add the following code to see how they work with strings, numbers, and lists.
## Augmented assignment with a string
greeting = 'Hello'
greeting += ' world!'
print(greeting)
## Increment a number by 1
number = 1
number += 1
print(number)
## Replicate list elements
my_list = ['item']
my_list *= 3
print(my_list)
Run the script from the terminal:
python3 main.py
The output will show the results of the augmented assignments:
Hello world!
2
['item', 'item', 'item']
Walrus Operator
In this step, you will learn about the Walrus Operator (:=), officially known as the Assignment Expression Operator. This operator, introduced in Python 3.8, allows you to assign a value to a variable as part of a larger expression.
It's useful for simplifying code where you need to use a value right after computing it.
Let's see it in action. Clear the main.py file and add the following code:
## The walrus operator assigns "Hello World!" to my_var
## and returns the value, which is then printed.
print(my_var := "Hello World!")
## The variable my_var is now available for use
print(my_var)
## You can also use it to re-assign a variable within an expression
print(my_var := "Hello")
print(my_var)
Execute the script:
python3 main.py
You will see the following output, demonstrating that the variable was assigned and its value was returned in the same expression:
Hello World!
Hello World!
Hello
Hello
Data Types
In this step, we'll get an overview of Python's nine core built-in data types. Understanding data types is crucial as they define the kind of values a variable can hold and the operations that can be performed on it.
| Data Type | Examples | Description |
|---|---|---|
| Numbers | ||
int |
-2, 0, 5 |
Whole numbers (integers) |
float |
-1.25, 0.0, 1.0 |
Numbers with decimal points |
complex |
2+3j |
Numbers with real and imaginary parts |
| Text | ||
str |
'a', "Hello!" |
Text and characters |
| Boolean | ||
bool |
True, False |
True or False values |
| None | ||
NoneType |
None |
Represents "no value" or "nothing" |
| Collections | ||
list |
[1, 2, 3] |
Ordered, changeable collections |
dict |
{'name': 'Alice', 'age': 30} |
Key-value pairs |
tuple |
(1, 2, 3) |
Ordered, unchangeable collections |
set |
{1, 2, 3} |
Unordered collections of unique items |
To see these data types in practice, clear your main.py file and add the following code. We will define variables of different types and then print their type using the built-in type() function.
## Numbers
age = 25 ## int
price = 19.99 ## float
coordinate = 2 + 3j ## complex
## Text
name = "Alice" ## str
## Boolean
is_student = True ## bool
## None
result = None ## NoneType
## Collections
scores = [85, 92, 78] ## list
person = {'name': 'Bob', 'age': 30} ## dict
coordinates = (10, 20) ## tuple
unique_ids = {1, 2, 3} ## set
print(type(age))
print(type(price))
print(type(name))
print(type(is_student))
print(type(scores))
print(type(person))
print(type(coordinates))
print(type(unique_ids))
Run the script to see the type of each variable:
python3 main.py
Your output should look like this:
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
<class 'list'>
<class 'dict'>
<class 'tuple'>
<class 'set'>
Concatenation and Replication
In this final step, you'll learn two fundamental string operations: concatenation and replication.
String concatenation is the process of joining strings together. In Python, adjacent string literals are automatically concatenated.
String replication uses the * operator to create a new string by repeating an existing one multiple times.
Let's try these operations. Clear the main.py file and add the following code:
## String concatenation: adjacent string literals are automatically joined.
## We use print() to display the result.
concatenated_string = 'Alice' 'Bob'
print(concatenated_string)
## String replication: repeat a string multiple times using the * operator.
replicated_string = 'Alice' * 5
print(replicated_string)
Now, run the script from the terminal:
python3 main.py
You will see the results of the two operations:
AliceBob
AliceAliceAliceAliceAlice
This demonstrates how easily you can manipulate strings in Python.
Summary
Congratulations on completing this lab! You have taken your first steps into the world of Python programming.
In this lab, you learned about several fundamental concepts:
- Math Operators: You practiced using operators like
+,*,/, and**and understood their precedence. - Augmented Assignment Operators: You discovered shortcuts like
+=and*=to write more concise code. - Walrus Operator: You were introduced to the
:=operator for assigning variables within expressions. - Data Types: You got an overview of Python's core data types, including numbers, strings, booleans, and collections.
- String Concatenation and Replication: You learned how to join and repeat strings.
These concepts are the building blocks for writing more complex and powerful Python programs. Keep practicing and exploring what Python has to offer!



