module1_python
module1_python
Logistics:
Press Shift + Enter keys to run each block of code on Google Colab environment or copy and paste the
codes/comments into a python file.
Conceptual introductions and explanations are provided in text blocks. Code-related explanations are
included as comments above the codes.
Exercises/practice problems are labeled in each module. For coding exercises, you could download an
external Python IDE (e.g. Anaconda) to program and test your implementation. One possible
implementation of the exercise are provided under the problem.
Learning Objectives:
1. Define and modify variables of various data types. Convert between data types.
2. Understand the characteristics and uses of each operation and the corresponding output.
3. Understand and correctly these statements for checking conditions.
1.1: Variables
Case-sensitive
MUST start with either a letter or underscore; CANNOT start with numbers.
CANNOT be the same name as Python keywords (e.g. class, finally, etc.)
do NOT specify type of information stored in the variable. (Refer to the following codes for an example.)
area = 0
https://cdn.evg.gov.br/cursos/338_EVG/htmls/modulo01_html01.html 1/10
27/12/2024, 16:38 module1_python
In [ ]: width
Out[ ]: 10
height
--------------------------------------------------------------------------
-
NameError Traceback (most recent call las
t)
<ipython-input-6-d56756f3e5e3> in <module>()
2 # ERROR CODE: "height" is not defined.
3
----> 4 height
In [ ]: Height
Out[ ]: 5
global = 1
global
# storing a string
helloMessage = "Hello World!"
first_name = "John"
# storing a char
character_example = 'a'
# storing a float
_newFloat = 1.0
In [ ]: helloMessage
In [ ]: character_example
Out[ ]: 'a'
https://cdn.evg.gov.br/cursos/338_EVG/htmls/modulo01_html01.html 2/10
27/12/2024, 16:38 module1_python
In [ ]: _newFloat
Out[ ]: 1.0
In [ ]: bool_Condition
Out[ ]: True
Out[ ]: int
In [ ]: type(helloMessage)
Out[ ]: str
In [ ]: type(bool_Condition)
Out[ ]: bool
type(width_float)
Out[ ]: float
type(width_int)
Out[ ]: int
Out[ ]: str
https://cdn.evg.gov.br/cursos/338_EVG/htmls/modulo01_html01.html 3/10
27/12/2024, 16:38 module1_python
Out[ ]: int
1.2: Operators
# Addition
print(5+23)
# Subtraction
print(100-25)
# Multiplication
print(5*10)
# Power/Exponent
# ** operator is equivalent to exponent
print(5**2)
# Division (float)
# Return the actual decimal value of division
print(36/4)
print(10/3)
# Division (int)
# Return an int. If the actual quotient is a decimal value, only whole numb
er is returned
print(10//3)
print(19//6)
28
75
50
25
25
9.0
3.3333333333333335
3
3
1
https://cdn.evg.gov.br/cursos/338_EVG/htmls/modulo01_html01.html 4/10
27/12/2024, 16:38 module1_python
foofoofoofoofoo
xxx
--------------------------------------------------------------------------
-
NameError Traceback (most recent call las
t)
<ipython-input-23-47a2cb16f654> in <module>()
1 # ERROR: compiler treats x as a variable, not a character
----> 2 print(x*3)
In [ ]: # ERROR: cannot concatenate an int to a string --> need to cast int to stri
ng
print("hello" + 5)
--------------------------------------------------------------------------
-
TypeError Traceback (most recent call las
t)
<ipython-input-24-8beae78f288a> in <module>()
1 # ERROR: cannot concatenate an int to a string --> need to cast in
t to string
----> 2 print("hello" + 5)
In [ ]: # Fix
print("hello " + str(5))
hello 5
hello world
https://cdn.evg.gov.br/cursos/338_EVG/htmls/modulo01_html01.html 5/10
27/12/2024, 16:38 module1_python
# Equality ==
# Note: MUST be Double equal signs, single equal sign is assignment
print(5 == 5.0)
True
True
True
True
True
False
True
True
False
In [ ]: # Comparators on Strings
print("hello" == "hello")
True
False
False
True
True
1.3.1: If Conditions
https://cdn.evg.gov.br/cursos/338_EVG/htmls/modulo01_html01.html 6/10
27/12/2024, 16:38 module1_python
Important Notices:
In [ ]: x = 7
y = 14
if (2*x == y):
print("y is double of x")
elif (x**2 == y):
print("y is the squared of x")
else:
print("y is NOT double of x")
y is double of x
In [ ]: x = 7
y = 49
if (2*x == y):
print("y is double of x")
elif (x**2 == y):
print("y is the squared of x")
else:
print("y is NOT related to x")
y is the squared of x
In [ ]: x = 7
y = 50
if (2*x == y):
print("y is double of x")
elif (x**2 == y):
print("y is the squared of x")
else:
print("y is NOT double nor squared of x")
https://cdn.evg.gov.br/cursos/338_EVG/htmls/modulo01_html01.html 7/10
27/12/2024, 16:38 module1_python
Python does NOT have an implementation for the switch cases, but one way to implement the switch case is
with the dictionary, a data structure that stores the key-value pair (Module 3).
The switch conditions are stored as keys in the dictionary, and actions stored as the value.
If there is a series of actions for each case, then consider writing a function for each case and use
the function calls as the value.
The default condition is manually listed as a key-value in the get().
In [ ]: def switcher(number):
Dial a number9
In [ ]: """
EXERCISE: implement the above switch case example using if/else conditions
Prompt: For each digit between 0-9, the program will print a confirmation
for the entered value or print "invalid inputs" for all other numbers.
"""
Out[ ]: '\nEXERCISE: implement the above switch case example using if/else conditi
ons\n\nPrompt: For each digit between 0-9, the program will print a confir
mation \nfor the entered value or print "invalid inputs" for all other num
bers.\n'
DO NOT LOOK AT THE SOLUTION BELOW BEFORE YOU TRY THE EXERCISE!
DO NOT LOOK AT THE SOLUTION BELOW BEFORE YOU TRY THE EXERCISE!
https://cdn.evg.gov.br/cursos/338_EVG/htmls/modulo01_html01.html 8/10
27/12/2024, 16:38 module1_python
DO NOT LOOK AT THE SOLUTION BELOW BEFORE YOU TRY THE EXERCISE!
DO NOT LOOK AT THE SOLUTION BELOW BEFORE YOU TRY THE EXERCISE!
DO NOT LOOK AT THE SOLUTION BELOW BEFORE YOU TRY THE EXERCISE!
DO NOT LOOK AT THE SOLUTION BELOW BEFORE YOU TRY THE EXERCISE!
DO NOT LOOK AT THE SOLUTION BELOW BEFORE YOU TRY THE EXERCISE!
DO NOT LOOK AT THE SOLUTION BELOW BEFORE YOU TRY THE EXERCISE!
DO NOT LOOK AT THE SOLUTION BELOW BEFORE YOU TRY THE EXERCISE!
DO NOT LOOK AT THE SOLUTION BELOW BEFORE YOU TRY THE EXERCISE!
DO NOT LOOK AT THE SOLUTION BELOW BEFORE YOU TRY THE EXERCISE!
DO NOT LOOK AT THE SOLUTION BELOW BEFORE YOU TRY THE EXERCISE!
DO NOT LOOK AT THE SOLUTION BELOW BEFORE YOU TRY THE EXERCISE!
DO NOT LOOK AT THE SOLUTION BELOW BEFORE YOU TRY THE EXERCISE!
https://cdn.evg.gov.br/cursos/338_EVG/htmls/modulo01_html01.html 9/10
27/12/2024, 16:38 module1_python
if number == '0':
print("Entered 0")
elif number == '1':
print("Entered 1")
elif number == '2':
print("Entered 2")
elif number == '3':
print("Entered 3")
elif number == '4':
print("Entered 4")
elif number == '5':
print("Entered 5")
elif number == '6':
print("Entered 6")
elif number == '7':
print("Entered 7")
elif number == '8':
print("Entered 8")
elif number == '9':
print("Entered 9")
else:
print("Invalid number!")
Dial a number9
Entered 9
In [ ]:
https://cdn.evg.gov.br/cursos/338_EVG/htmls/modulo01_html01.html 10/10