Tip
Learning smaller parts of code will help you to implement specific types of functionalities in larger projects!
Let’s Return to Literals (e.g., numbers, strings, booleans, etc.) and work with them in interesting ways using print()
.
Note
In Python, print statements with an f
prefix before the opening quotation mark denote f-strings, also known as formatted string literals. F-strings provide a concise and readable way to embed expressions inside string literals for formatting output.
Note
Curiously, both of the below print statements print out the same results, but the code is not the same …
firstName = "Robert"
lastName = "Paulson"
print(f"His name is: {firstName} {lastName}!") # f for auto formatting
print("His name is:",firstName, lastName,"!") # original structure
Output:
His name is: Robert Paulson!
His name is: Robert Paulson ! # A space appears?!
Operators let you do math, compare values, and more!
Important
Examples:
Arithmetic: +
, -
, *
, /
, **
(power)
Comparison: ==
, !=
, <
, >
Assignment: =
, +=
, -=
Variables and operators are the backbone of calculators, games, and simulations!
Loops repeat actions. Use for
and while
loops.
Conditionals let your code make decisions using if
, elif
, and else
.
Loops and conditionals can be used to determine positive, negative numbers, or zeros. Neat-O!
Examples:
Output:
Positive
You can nest loops, one inside the other.
Examples:
Output:
Positive
Logical operators use True
and False
to determine outcomes.
Examples:
Output:
Both conditions are true At least one condition is true a is not less than 5
We now look at how we can manipulate strings
A list is a fundamental data structure in Python used to store an ordered collection of items.
Lists maintain the order of elements, with each item accessible by its index (starting from 0).
Lists are mutable, so their contents can be changed after creation (items can be added, removed, or modified).
Lists can hold items of different data types, including other lists.
Lists are created using square brackets, with elements separated by commas.
Examples:
my_list = [1, 2, 3, 4, 5] # definition
print(my_list[0]) # Access first element
my_list.append(6) # Add an element to the end
print(my_list) # [1, 2, 3, 4, 5, 6]
my_list.remove(3) # Remove element with value 3
print(my_list) # [1, 2, 4, 5, 6]
print(len(my_list)) # Length of the list
Output:
1
[1, 2, 3, 4, 5, 6]
[1, 2, 4, 5, 6]
5
A dictionary is a built-in data structure in Python used to store key-value pairs.
Dictionaries are unordered collections (as of Python 3.6+, they maintain insertion order), where each value is accessed using its unique key.
Dictionaries are mutable, so their contents can be changed after creation (items can be added, removed, or modified).
Keys in a dictionary must be unique and immutable (such as strings, numbers, or tuples), while values can be of any data type.
Dictionaries are created using curly braces {}, with key-value pairs separated by commas and a colon between each key and value.
Examples:
my_dict = {"name": "Alice", "age": 25, "is_student": True} # definition
print(my_dict["name"]) # Access value by key
my_dict["age"] = 26 # Modify value
my_dict["city"] = "New York" # Add new key-value pair
print(my_dict) # {'name': 'Alice', 'age': 26, 'is_student': True, 'city': 'New York'}
del my_dict["is_student"] # Remove a key-value pair
print(my_dict) # {'name': 'Alice', 'age': 26, 'city': 'New York'}
print(len(my_dict)) # Number of key-value pairs
Output:
Alice
{‘name’: ‘Alice’, ‘age’: 26, ‘is_student’: True, ‘city’: ‘New York’}
{‘name’: ‘Alice’, ‘age’: 26, ‘city’: ‘New York’}
3
Challenge:
Create two variables, a
and b
, assign them integer values, and print their sum, difference, product, and quotient.
Challenge:
Create a list of five numbers. Print the largest number in the list.
Challenge:
Create a dictionary with three key-value pairs representing a person’s name, age, and city. Print each key and its value.
Challenge:
Use a for loop to print all even numbers from 1 to 10.
Challenge:
Use a while loop to keep asking the user to enter a number until they enter a negative number.