Introduction to python - 1 2
Introduction to Python
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.
Expressions in Python
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>.
Assignments, Names, and Namespaces
Assignment Statements
Assignment statements assign objects to variable names.
Examples:
x = 10x = "10"y = 30x = "This is a test"z = 20x = y + zResulting inxbeing 50 andybeing 30.
Variables in Python are dynamically typed; their data type can change upon reassignment.
Importance of Variable Names
Variable names should:
Be unambiguous and descriptive.
Be easy for readers to understand.
Be case-sensitive and adhere to
snake_caseformatting.Not be reserved words (keywords).
Examples of Bad Variable Names
Examples include:
pikachu = "Carlota"potato = 2025True = Falseglobal = "The world"Person name = "Laura"
Good Variable Names
Good examples that follow the guidelines include:
user = "Carlota"year = 2025graduation_date = "20/05/2028"new_pi_number = 3.546global_variable = "The world"
Namespaces in Python
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
Variable Scope and the LEGB Rule
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
Conditional statements in Python:
If [condition]
Elif [condition]
Else
Ifandelifrequire conditions to execute their blocks, whereaselseexecutes without a condition.
Example of Conditional Statements
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 in Python
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.
Iteration Examples
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).
Exercises
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.