This section introduces Python programming within the context of the class "Programming with Data S2-2025" at ESADE. The motto "Do Good. Do Better" encapsulates the learning philosophy of the program.
In Python, expressions consist of a combination of values, variables, and operators that yield a result of a specific type when evaluated.
The general syntax to form an expression is: <value/variable> <operator> <value/variable>
.
Results of expressions can be assigned to a variable, expressed as: result = <value/variable> <operator> <value/variable>
.
Assignment statements assign objects to variable names.
Examples:
x = 10
x = "10"
y = 30
x = "This is a test"
z = 20
x = y + z
Resulting in x
being 50 and y
being 30.
Variables in Python are dynamically typed; their data type can change upon reassignment.
Variable names should:
Be unambiguous and descriptive.
Be easy for readers to understand.
Be case-sensitive and adhere to snake_case
formatting.
Not be reserved words (keywords).
Examples include:
pikachu = "Carlota"
potato = 2025
True = False
global = "The world"
Person name = "Laura"
Good examples that follow the guidelines include:
user = "Carlota"
year = 2025
graduation_date = "20/05/2028"
new_pi_number = 3.546
global_variable = "The world"
A namespace is a collection of symbolic names mapped to objects, similar to a dictionary.
There are four types of namespaces in Python:
Built-In
Global
Enclosing
Local
The Python interpreter resolves variable names based on the order of namespaces with the LEGB rule:
Local: Searches the innermost local scope of the function.
Enclosing: Searches the enclosing function scopes.
Global: Searches the global scope.
Built-in: If the variable is not found, it checks built-in names.
Conditional statements in Python:
If [condition]
Elif [condition]
Else
If
and elif
require conditions to execute their blocks, whereas else
executes without a condition.
An example of a conditional statement:
input_value = int(input("Enter a number: "))
if input_value < 20:
print("It's satisfying the first condition")
elif input_value < 50:
print("It's satisfying the second condition")
else:
print("Other")
Iterations may be achieved through:
While loops: Execute as long as a condition is true.
For loops: Iterate over a data structure (list, tuple, dictionary, etc.).
Control statements:
break
: Stops the loop execution.
continue
: Skips the current iteration and continues with the next.
Iterations may be utilized for:
Searching for a value in an array.
Asking for user inputs until a valid entry is received (e.g., guessing a number).
Create a program to prompt the user for their name and total portions with specified conditions for calculating cost (5.90 per portion).
Write a program to determine magnitudes of user-entered integers.
Develop a calculator that executes specified operations based on user input.
Implement a temperature conversion program from Fahrenheit to Celsius.
Ask for hourly wage and calculate daily wages while considering Sundays.
Suggest clothing based on weather conditions.
Compare names and ages of two individuals.
By completing iterative exercises, students will solidify their understanding of Python syntax, logic flow, and best practices for coding. The material encourages practical application through the hands-on development of functions and programs.