Python Lesson 3 Variables, Types and Lists
Python Lesson 3 Variables, Types and Lists
program's progress.
The print() Function
• Example:
print("Hello World.")
print(“The result is”, 10+5)
print("Welcome to Python programming!")
Indentation and Code Structure
temperature = 30
if temperature > 25: for i in range(0, 5):
print("It's a warm day.") print("Hello World.")
x = 10
if x > 5:
print("x is greater than 5") # This line is indented and part of the `if` block
Variables
x = 5 # x is an integer
x = "Hello" # Now x is a string
Variables
• Integers (int): Whole numbers, positive or negative
Example: x = 5
Example: y = 5.0
double quotes
# String concatenation
hello = “hello”
world = “world”
helloworld = hello + world # Concatenates with a space in between
print(helloworld) # This will output: hello world
Simultaneous Variable Assignment
Simultaneous Variable Assignment in Python refers to the ability
to assign values to multiple variables in a single line of code
using a tuple-like syntax. This allows for more concise and
readable code.Concatenating Strings:
x, y = 10, 20 # Assigns 10 to x and 20 to y at the same time
a, b = 3, 4
print(a,b)
Mixing operators
Mixing operators between numbers and strings is not supported
Storing Items: Lists can hold any type of item, like numbers, words, or
even other lists. You can add as many items as you want.
Easy to Loop Through: You can easily go through each item in a list one
by one.
Example of Creating and Using a List
• Creating a List: You can create an empty list using square
bracket
mylist = [ ] # This creates an empty list
• Adding Items: You can add an item to the list using the
append() method
mylist.append(1) # This add the number
1 to the list
Example of Creating and Using a List
• Counting Starts at Zero: Lists start counting from zero. This
means the first item in the list is at position 0.
print(mylist[0]) # This prints the first item in the list, which
is 1