Final Programming
Final Programming
VARIABLES
In programming, a variable is a memory space where data is stored and
they retrieve the data that a program uses.
Each variable must have a name by which it can be identified and referred to.
to her, during the development of a program.
The variable name cannot match the names of the commands.
assigned to this programming language, in addition to not containing
blank spaces.
Variables that store numbers
○Integers → int
Real numbers/decimals → float
●Variables that store text (in "") →str
RESERVED WORDS
● They are identifiers for exclusive use of the programming language, which do not
they can be used to identify and name variables, methods, objects or
any element within our code.
When trying to use reserved words outside of their assigned usage,
what we will get is a compilation error.
Hello
message += " "
message += 'Cami'
message
RESULT
Hello Cami
1
using commas is efficient, this way spaces are added and we
You can combine variables of both text and numbers.
Hello
espacio
Cami
print(message + space + name)
Hello
Cami
message
RESULT
Hello Cami
Hello Cami
ARITHMETIC OPERATORS
They are those who manipulate numerical data, both whole numbers and
decimal numbers also known as real numbers.
These operators are the simplest of all, they are used to perform
basic arithmetic operations, such as addition, subtraction, multiplication,
divisions, modulus or remainder, and exponentials.
Sum (+)
Subtract (-)
Multiplication (*)
Exponent (**)
Module (%)
Division (/)
Integer division (//)
COMMENTS
Significant annotations for programmers, but ignored by the
program.
○ ' ' → single quotes, as long as they are not assigned to a variable,
as they are interpreted as a null value and for that reason Python does not
take into account.
triple quotes.
○#
Hello, I am Cami
Hello, I am
Cami
Hello, I am Cami
DATA TYPES
Integers and Longs
2
Integers are those that do not have decimals, both positive
as negatives
They can be represented by the tipoint (integer) or the long type (long).
●Floating or real
Floating point numbers are those that have decimals, both positive and
negatives
They are expressed through the type float.
●Chains or Strings
They are nothing more than a text enclosed in single, double, or triple quotes.
(allowing line breaks).
Boolean
It can only have two values → True or False. These values are
especially important for conditional expressions and loops.
They are expressed using the type bool.
RESULT:
15
15.5 <class 'float'>
Ernesto
True
EXAMPLE → if I specify the data type, for example int or float, and I introduce another type
with different data, it will give me an error.
3
print("String: ", word)
print("Integer: ", num_int)
print("Float: ", num_float)
RESULT
introduce a word: hello
Enter an integer: 3
Enter a float number: 2.5
hello
Enter: 3
Float: 2.5
RESULT:
What is your name?: cami
Hello Cami, we are going to do an addition.
Please enter the first value: 3
Please enter the second value: 5
The result of the sum is: 8
CONDITIONALS
It is a instruction o group of instructions what him execute when a one
a logical condition is set for the program. When that condition is met, the
program executes the instruction that has been assigned to this condition
They help us control decision-making within a program, making use of
of logic
They check if a condition is true or false and based on that, they carry out.
an action
Our program evaluates the condition and there are two possible outcomes: it is met.
(true) or it does not fulfill (false)
4
Everything below delif (with the space) is part of the condition.
num_uno =5
if num_uno == 5:
The number is five
End.
RESULT
The number is five
End.
number_one = 10
if num_uno == 5:
The number is five
End.
RESULT:
End.
In this case, 10 is not equal to 5, so the condition is not met and it goes for the
false path. Therefore, it goes from the conditional statement and moves on to the instruction
that no longer belongs to said sentence
if average >= 6:
Congratulations
End
5
RESULT
System for calculating a student's average.
To begin, what is your name?: cami
Cami, what is your math grade?: 9
Cami, what is your chemistry grade?: 8
Cami, what is your biology grade?: 7
Congratulations Cami, you passed with an average of: 8
End
num_uno=5
if num_uno == 5:
The number is five.
else:
The number is NOT five.
End.
RESULT:
The number is five.
The end.
num_uno=10
6
if num_uno == 5:
The number is five.
else:
The number is NOT five.
Fin.
RESULT
The number is NOT five.
Fin.
if average >= 6:
Congratulations
print("Congratulations " + name + " you passed with an average of: ",
round(average,2)
else:
print("We are sorry " + name + " you failed with: ", average)
print("We are sorry " + name + " you failed with: ", round(average, 2))
End
RESULT:
System to calculate a student's average.
To start, what is your name?: cami
Cami, what is your math grade?:2
Cami, what is your chemistry grade?: 5
Cami, what is your biology grade?:1
We regret to inform you, Cami, that you have failed with an average of: 2.6666666666666665
We regret to inform you that Cami has failed with an average of: 2.67
End
7
In the moment in what some of the conditions if may it be fulfilled, self execute the
instruction or instructions corresponding to said condition and the execution of
the conditional sentence will end
If [logical condition] :
Instruction
Elif [logical condition] :
[Instruction]
Else:
[Instruction]
CON
CON
num_uno=1
if num_uno == 1:
The number is ONE
elif num_uno == 2:
The number is TWO
else:
The number is unknown
End
RESULT
The number is ONE
End
num_uno=2
if num_uno == 1:
The number is ONE
elif num_one == 2:
The number is TWO
else:
The number is unknown
Fin
RESULT
The number is TWO
8
End
num_uno=3
if num_uno == 1:
The number is ONE
elif num_one == 2:
The number is TWO
else:
The number is unknown
End
RESULT
The number is unknown
End
if num_uno == 1:
The number is ONE
elif num_uno == 2:
The number is TWO
elif num_uno == 3:
The number is THREE
elif num_one == 4:
The number is FOUR
elif num_one == 5:
The number is FIVE
else:
This program can only convert up to the number 5
End.
RESULT:
Number to words converter!
What number do you want to convert? 3
The number is THREE
The End.
9
They consist of having a conditional instruction within another, and depending on
if the condition of the first conditional sentence is met or not met, it
will execute another conditional statement.
In this way, we will have a condition within another condition, expanding the
number of options for our programs to solve a problem,
regardless of the number of situations that arise.
Converter
Options menu:
Press 1 to convert from number to word.
Press 2 to convert from word to number.
if option == 1:
Number to word converter
elif option == 2:
print("Word to number converter")
else:
Option not available
FIN.
RESULT
10
Converter
Menu of options:
Press 1 to convert from number to word.
Press 2 to convert from word to number.
What is your desired option?
Word to number converter
What is the word you want to convert to a number: ?two
The number is 2
END.
------------------------------------------------
Converter
Menu of options:
Press 1 to convert from number to word.
Press 2 to convert from word to number.
What is your desired option?
Number to word converter
What is the number you wish to convert to word: 5
The number is FIVE
END.
RELATIONAL OPERATORS
Relational operators are symbols used to compare two values.
and they are generally used in conditional sentences for making
decisions.
When using relational operators within a conditional statement, if the
the result of the comparison is correct, the expression or condition is considered
true, and if not, it will be false
11
if num_one == num_two:
It's the same...
if num_uno != num_dos:
It is different...
if num_one < num_two:
It's smaller...
if num_one > num_two:
It is greater...
if num_one <= num_two:
It is less than or equal...
if num_one >= num_two:
print("Is greater than or equal...")
RESULT:
Enter two numbers to compare:
Enter the first number: 15
Enter the second number: 2
The numbers to compare are: 15 and 2
It's different...
It is greater...
is greater than or equal to...
LOGICAL OPERATORS
Sometimes, it is necessary to use more than two logical conditions within one.
same condition, which means we find ourselves in the need to implement multiple
relational operators to create a much more complex logical expression
However, it is not possible to place two logical conditions within the same one.
condition, without the support of any element that indicates to our programs,
that the union of two or more logical conditions will be carried out within the same
expression.
For this type of situation, there are logical operators, which,
they allow grouping logical conditions within the same condition. That is,
with logical operators we have the possibility to use multiple
relational operators within the same logical condition.
CONJUNCTION AND
DISJUNCTION OR
DENIAL NOT
AND
(num_uno = 5)
ifnum_one == 5 How do I do to ifnum_uno== 5and ifnum_dos >= 5:
join these two Both conditions have been
(num_uno = 5) → conditions
united
ifnum_dos >= 5 logics in one
sole expression?
12
(num_uno = 5)
ifnum_uno== 5 ifnum_uno == 5 and ifnum_dos >= 5:
How do I do to Both conditions have been
(num_uno = 2) → join these two
united
ifnum_dos >= 5 conditions
logics in one else:
sole expression? One or both conditions not
they have been fulfilled
OR
(num_uno = 5)
ifnum_uno == 5 It meets one or ifnum_uno== 5or ifnum_dos >= 5:
both conditions Both conditions have been
(num_uno = 2) →
united
ifnum_dos >= 5
(num_uno = 8)
ifnum_uno== 5 Does not meet any. ifnum_uno== 5or ifnum_dos >= 5:
from two Both conditions have been
(num_uno = 2) → conditions
united
ifnum_dos >= 5 else:
Does not meet any of the
two conditions)
#and
Conjunction (and)
#or
Disjunction (or)
13
RESULT:
Conjunction (and)
Write a number greater than 2 and less than 5:3
The number 3 meets the condition
Disjunction (or)
To comply with the condition write 'yes' or 'yes':yes
The condition has been fulfilled
PRACTICAL EXERCISES:
The multinational company Rappi requests a system that determines the days of
holidays to which a worker is entitled, taking into account the following
characteristics:
There are three departments within the company with their respective keys:
1. Customer service department (key 1)
2. Logistics Department (key 2)
3. Management (key 3)
The system must request the 'Name', 'Department key', and 'Tenure' of the
worker from the keyboard
Subsequently, the program must display a message on the screen containing the
worker's name
if department_key == 1:
if employee_seniority == 1 and employee_seniority <2:
The employee
vacation
elif employee_seniority >= 2 and employee_ancient <=6:
14
The employee
vacation
elif employee_seniority >= 7:
The employee
vacation
else:
The employee
elif department_key == 2:
if employee_seniority == 1 and employee_ancientness <2:
The employee
vacation
if employee_seniority >= 2 and employee_seniority <=6:
The employee
vacation
elif employee_seniority >= 7:
The employee
vacation
else:
The employee
elif department_key == 3:
if employee_seniority == 1 and employee_seniority <2:
The employee
vacation
if employee_seniority >= 2 and employee_seniority <= 6:
The employee
vacation
if employee_seniority >= 7:
The employee
vacation
else:
The employee
else:
The department key does not exist, please try again.
ANSWER:
Rappi's vacation control system
Please enter the employee's name: Cami
Please enter the department key:3
Please enter the number of working years 2.5
Employee Cami is entitled to 20 days of vacation.
EVEN NUMBER → is an integer that is divisible by two. That is, at the moment
that an integer divided by two has a remainder of zero.
ODD NUMBER → is an integer that is not divisible by two. That is to say, that when
the moment an integer is divided by two, the modulus or remainder is equal to
one
Develop a program that requests an integer number from the user via keyboard,
subsequently, the program must determine and indicate through a message whether the
The entered number is even or odd.
15
The on-screen message should display the phrase the number is even or odd, along with the
number that the user entered from the keyboard, for example:
The number 8 is even
The number 5 is odd
if number % 2 == 0:
print("The number ", number, " is even")
elif number % 2 == 1:
print("The number ", number, " is odd")
RESULT
Program that determines if a number is even or odd
Please enter an integer: 250
The number 250 is even.
Develop a program that requests three integers from the user via keyboard.
subsequently, the program must determine and indicate through a message in
screen, which of the three numbers is the largest.
The message on screen should show the number that turned out to be the largest of the
three, for example:
The number 10 is the largest of the three.
RESULT
Program to determine which is the largest number of the three
Enter the first number: 1
Enter the second number: 2
Enter the third number: 3
The number 3 is the largest number of the three.
16
ASSIGNMENT OPERATORS
NAME OPERATOR IMPLEMENTATION EXPRESSION
EQUIVALENT
WHILE LOOP
● Sometimes when we are developing a program, we encounter the
need to execute one or more lines of code repeatedly, with which
the most logical option is to duplicate the code of these instructions so that
the program completed the assigned task.
However, this alternative is not the most optimal as it duplicates code.
It can generate various problems such as, for example, unnecessary files.
more extensive and difficult to understand when wanting to carry out some
modification or update of the same.
In this situation, in programming we have repetition statements.
code, which allows us to execute a series of instructions or lines of
controlled manner code within our programs, to which they are
known as cycles or loops.
A cycle or loop allows the instructions to be executed repeatedly.
code lines indicated by the programmer, thus, there is no
need to duplicate lines of code to execute them more than once.
The while loop allows for the repeated execution of a group of instructions.
while a condition is met, that is, while the loop condition or
the loop meets the instructions will continue to execute
17
while[condition]:
[instruction]
X=1
while X < 4:
print(X)
X+=1
End
RESULT
1
2
3
End
What this instruction does is keep adding 1, and since the condition is that it is < 4,
when it is not fulfilled, that is, it is > 4, the program ends.
X=1
while X < 11:
Cami
X+=1
End
RESULT:
Rug
Cami
Cami
Cami
Cami
Cami
Cami
Camie
Cami
Cami
End
BREAK SENTENCES
The break statement is used to stop the execution of an iteration and exit.
from her, which will allow the program to continue executing the code
that is outside our loop.
CON
CON
18
#example for break
print("while with the break statement")
counter = 0
while counter < 10:
counter += 1
if counter == 5:
break
Current value of the variable:
End of the program, the break statement has been executed.
RESULT
while with the break statement
Current value of the variable: 1
Current value of the variable: 2
Current value of the variable: 3
Current value of the variable: 4
End of the program, the break statement has been executed.
SENTENCES CONTINUE
It allows to stop the current iteration and return to the beginning of the loop to
perform a new iteration, if the condition that governs our loop is
continues to fulfill (an iteration is the repetition of a segment of code within
of a program)
CON
CON
#example to continue
print("while with the continue statement")
contador =0
while counter < 10:
counter += 1
if counter == 5:
continue
Current value of the variable: , counter
RESULT
while with the continue statement
Current value of the variable: 1
Current value of the variable: 2
Current value of the variable: 3
Current value of the variable: 4
Current value of the variable: 6
Current value of the variable: 7
Current value of the variable: 8
19
Current value of the variable: 9
Current value of the variable: 10
len(" H o l a ")
0 1 2 3 4
#option 1
print("Hello has", len("Hello"), ("characters."))
#option 2
length = len("Argentina")
Argentina has
RESULT:
Hello has 4 characters.
Argentina has 12 characters.
FOR LOOP
It is a control structure that allows us to repeat a block of instructions.
(sentences), a certain number of times
An iterable object is one that allows you to traverse its elements one by one. Such as
for example, a string of characters. For example, the sentence 'Hello Cami
traverse character by character
Hello
for character in string:
print(character)
End of the program.
RESULT:
H
20
o
l
a
End of the program.
THE CLASERANGE
Generate immutable number sequences, that is, ones that cannot be modified.
these sequences are generated from a previously established range
It is generally used as an iterable object within the syntax of the for loop.
for loop, with which the respective iterations are achieved
It allows us to work with a minimum of one argument and a maximum of three.
arguments simultaneously. Thus, we can decide the number with which
It will start and end the sequence of numbers, and at the same time, indicate the increment.
the decrement between a number and the next
● range(inicio, parada, paso)
start: is an integer value that indicates the number from which it
it will start generating the sequence of numbers, and this number will always form
part of the sequence.
○ stop: it is an integer value that indicates the number up to which it will go
generate the sequence of numbers, and this number will never be part of the
sequence. For example, if it's 10, it will go from 0 to 9.
○ step: it is an integer value, which indicates the increase or decrease of the
numerical succession between one number and the next
5, 6, 7, 8, 9, 10
range(0, 6) → 0, 1, 2, 3, 4, 5
0, 2, 4, 6, 8, 10
0, 3, 6, 9
range(10, 0) → impossible. it returns nothing. since the increment is 1, it never goes to
reach zero.
range(10, 0, -1) → 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
RESULT
1
2
3
4
End of the program
21
Develop a program that displays the multiplication table of a number on the screen.
anyone, this number should be requested and entered from the keyboard by the
user
Use the for loop
The table must contain the multiplications from 0 to 10.
RESULT
Which multiplication table do you want to see? 8
The table of 8 is:
8x0=0
8x1=8
8 x 2 = 16
8 x 3 = 24
8 x 4 = 32
8 x 5 = 40
8 x 6 = 48
8 x 7 = 56
8 x 8 = 64
8 x 9 = 72
8 x 10 = 80
LISTS
Data structures are those that allow us to organize information.
efficiently, and thus, design alternative solutions for a specific
problem
One of the simplest data structures that exists are lists.
Lists are used to store sets of information, in this way,
create a collection of ordered elements, and in turn, these elements can either
no to be related between yes, it to say a list can to be homogeneous o
heterogeneous.
Homogeneous lists:
All the elements that make up the list are of the same data type.
○ lista_homogenea = ["Ari","Cami","Manu"]
Heterogeneous lists:
All the elements that make up the list are of different types.
date
○ lista_heterogenea = ["nombre",20, 1.70, True]
Additionally, the lists they have the characteristic of to be mutable, it is to say, what you
content can be modified after it has been created
● list =[]
Homogeneous list
numeros_enteros = [1,2,3,4,5]
22
print(integers)
numeros_decimales = [1.5,2.5,3.3,4.9]
print(decimal_numbers)
valores_boleanos = [True,False,False,True]
print(boolean_values)
RESULT:
Homogeneous list
['a', 'e', 'i', 'o', 'u']
[1, 2, 3, 4, 5]
[1.5, 2.5, 3.3, 4.9]
[True, False, False, True]
Heterogeneous lists
When we work with lists, surge the necessity of access to the elements
content within it, as this way we can consult and even
modify its content
For access a the elements of a list, is necessary to support us of the
indices, the which the they will indicate to us program the position exact del
element we want to work with from the list.
Complete list:
23
print("Brand -1:", brands[-1])
print("Brand:", brands[:])
RESULT:
Accessing the positions of a list
Lista completa: ['Apple', 'Samsung', 'Lenovo', 'HP']
How many elements are in the list?: 4
Samsung
Brand 3: HP
HP
Samsung
Marca 1 a 3: ['Samsung', 'Lenovo']
Marca 2: ['Apple', 'Samsung']
Marca 1: ['Samsung', 'Lenovo', 'HP']
Marca: ['Apple', 'Samsung', 'Lenovo', 'HP']
24
● The syntax to use the insert() method is as follows:
list_name.insert(position, new_element)
○ letras = [“b”, “d”, “f”, “g”]
letters.insert(0, "a")
it would be as follows: letters = ["a", "b", "d", "f", "g"]
○ letras = [“a”, “b”, “d”, “f”, “g”]
letters.insert(2, "c")
it would be as follows: letters = ["a", "b", "c", "d", "f", "g"]
○ letras = [“a”, “b”, “c”, “d”, “f”, “g”]
letters.insert(4, "e")
it would be as follows: letters = ["a", "b", "c", "d", "e", "f", "g"]
Nested Lists
Nested lists are lists within other lists.
→ lista = [1, “a”,True,[1,2,3]]
list[3][1] → first access position 3 and then position 1
It shows you the → 2
→ lista = [1,“a”,True, [1,2,[“f”, “g”, “h”]]]
list[3][2][0] → first access position 3, then 2, and then 0
It shows you the → f
25
Read the student ID numbers of a course and their final exam grades.
The end of the load is determined by entering -1 as the file number.
It must be validated that the entered grade is between 1 and 10. Reading completed.
data, inform
Number of students who passed with a grade of 4 or higher
Number of students who failed the exam. Score lower than 4
Average grade and the files that exceed the average
Then it is requested to display a list of files and grades ordered in a way
ascending according to the file number.
Resolve in two ways: Using two parallel lists and using a matrix of
two rows.
legajo =0
note = 0
alumnos = []
cant_aprobados =0
cant_desaprobados=0
total_notas =0
if len(students) > 0:
average = total_grades / len(students)
else:
average = 0
legajos_arriba_promedio = []
for file, student notes:
if grade > average:
average_files_above.append(file)
RESULTS:
Enter your file number (-1 to finish):1161066
Enter your final exam grade: 6
Enter your file number (-1 to finish):1161098
26
Enter your final exam score: 4
Enter your file number (-1 to finish):1161027
Enter your final exam grade: 5
Enter your file number (-1 to finish):1161926
Enter your final exam score: 10
Enter your file number (-1 to finish):1161034
Enter your final exam grade: 9
Enter your file number (-1 to finish):-1
Cantidad de alumnos que aprobaron: 5
Number of students who failed: 0
Average grade: 6.8
Legajos con nota mayor al promedio: [1161926, 1161034]
27
import random
while True:
Generate secret four-digit number
figures
Guess the four-digit secret number!
attempts = 0
while True:
guess = int(input("Enter your attempt (-1 to quit):
if guess == -1:
break
attempts += 1
if guess == secret_number:
print("Congratulations! You guessed the number in" , attempts, "attempts!")
if attempts < best_score:
Enter your document number:
best_score = attempts
best_scores.append((attempts, document_number))
break
elif guess < secret_number:
The entered number is greater.
else:
The entered number is smaller.
High scores:
for score, document in best_scores[:5]:
Attempts:
Read numbers that represent the ages of a group of people, ending the reading
When the number -1 is entered. Print how many are under 18 years old, how many
they are 18 years old or older and the average age of both groups. Discard values that do not
represent a valid age. (A valid age is considered to be between 0 and 100).
menores_de_18 =0
mayores_de_18 =0
suma_menores_de_18 =0
suma_mayores_de_18 =0
28
while age != -1:
if age >= 0 and age <=100:
else:
under_18 += 1
sum_under_18 += age
else:
The age is invalid, please enter another one between 0 and 100:
if adults_over_18 > 0:
average_of_over_18 = sum_of_over_18 / over_18
The average age of the elderly is:
if minors_under_18 >0:
average_minors = sum_of_minors_under_18 / minors_under_18
The average age of the minors is:
RESULT:
Enter the age12
Enter another age 32
Enter another age 45
Enter another age 76
Enter another age 202
the age is invalid, please enter another between 0 and 100:
Enter another age 102
the age is invalid, please enter another one between 0 and 100:
Enter another age32
Enter another age-1
Those under 18 are: 1
Those over 18 are: 4
The average age of the elders is: 46.25
The average age of the minors is: 12.0
Read three numbers D, M, and A corresponding to the day, month, and year of a date, and a
an integer number N that represents a quantity of days. Create a program that prints
the new date that results from adding N days to the given date. Keep in mind the years
leap years as detailed in exercise 7 of practice 3.
A leap year is divisible by 4, with the exception of years that are divisible
for 100 but not for 400
def is_leap(year):
return ( ( year %4 == 0 and year %100 != 0) or year %400 == 0 )
#30 days= 4, 6, 9, 11
29
31 days = 1, 3, 5, 7, 8, 10, 12
print("The new day is: ", day, " / ", month, " / ", year)
RESULT
Enter a day: 7
3
Enter a year: 2000
Enter the number of days to add: 32
The new day is: 8 / 4 / 2000
Read an integer and display a message informing how many digits it contains.
Keep in mind that the number can be negative. Example: If it is entered
12345 should print 5.
30
length = len(str(abs(number)))
RESULT:
Enter an integer 9056
The number contains 4 digits.
#432
numero_invertido =0
if number < 0:
inverted_number *= -1
print(inverted_number)#234
notes[0][1]
RESULT
Enter an integer: 432
234
unidad = []
metros = []
Enter the value of the expenses per square meter:
31
area=float(input("Enter the number of square meters: "))
unit.append(unit_number)
metros.append(surface)
total_superficie = 0
for area in square meters:
total_area += area
32