Lesson 1 Python Basics
Lesson 1 Python Basics
TECHNOLOGY
SCHOOL OF ECONOMICS
PRACTICE 3.1
Step 2: Print the values of the variables according to the preset format. In which:
print(...): This is the command to print the content to the screen.
f'...': is a format-string, which allows you to insert the values of variables
directly into the string of characters. Variables or expressions need to be placed
in pairs of {}.
{variable_1}, {variable_2}, and {variable_3}: are placeholders in the string
where the values of variable_1, variable_2, and variable_3 will be inserted
(these variables have been assigned values).
Step 5: Sort the list and find the length of the list
list_2.sort(reverse=True)
Description: The sort() method is used to sort elements in a list. The
reverse=True parameter specifies that the list will be sorted in descending order
(from largest to smallest).
Result: After executing this command, the list of list_2 will be sorted in
descending order
length_list = len(list_2)
Description: The len() function is used to calculate the length (number of
elements) of a list_2 list and save the result to the length_list variable.
Result: In this case, the list_2 list has 5 elements, so length_list will be
assigned a value of 5.
for i in list_2:
Description: This is a for loop used to iterate through each element in the list_2
list. In each iteration, the value of the current element in the list is assigned to
the variable i.
Uses: This loop will perform operations (in this case, print) for each element in
the list_2 list.
customer_age = list(range(10,90,3))
Description: Create a customer_age list containing integers from 10 to 90, with
a step of 3.
o range(10, 90, 3) produces a sequence of numbers starting at 10, ending
before 90, with a step of 3.
o list(...) converts this sequence of numbers into a list.
if key in friend:
Description: Check if the key (username) is in the friends list.
o If it does, it will proceed to print according to the print command
(f'Hello {key}, your favorite programming language is {value}')
Step 9: Define the function to print out the full name based on the input parameters of
first name and last name
Step 10: Define the function to print out the age based on the input parameter as age
(assuming the current year is 2024)
def cal_age(birth_year):
Description: This is the syntax for defining a function in Python. The name of
the function is cal_age.
Parameter: This function has a parameter:
o birth_year: Used to get the value of the user's year of birth.
Step 11: Define the function to find and return the largest value in the list with the
following main logic
Declare the function max_value find the largest value in the numbers list.
Implement:
Assign the first value of the list to the max_num variable.
Iterate through all the elements in the list. If the current element is larger than
max_num, update the max_num with that value.
After the iteration is complete, return the corresponding max_num value which
is also the largest value in the list.
Step 12: Define the function
The function odd_even classifies the numbers in the input numbers list into odd and
even numbers:
Implement:
Initialize two empty lists: odd_numbers for odd numbers and even_numbers
for even numbers.
Repeat through each number in numbers. If that number is divisible by 2 (even
number), add even_numbers; if not, add odd_numbers.
Print the list of odd and even numbers after the end of the lpawj round
(even_number and odd_number respectively)