Introduction to python - 1
Introduction to Python Programming
The course focuses on programming with data for the Spring term of 2025. It aims to equip students with essential Python programming skills.
Expressions in Python
Definition and Syntax
An expression in Python combines values, variables, and operators that can be evaluated to produce a result of a certain type.
Syntax for expressions:
<value/variable> <operator> <value/variable>
.Example of assigning an evaluated expression to a variable:
result = <value/variable> <operator> <value/variable>
.
Assignments, Names, and Namespaces
Assignment Statements
Assignment statements are utilized to assign objects to names. Examples of assignments:
x = 10
x = "10"
y = 30
x = "This is a test"
z = 20
x = y + z
It's crucial in Python that variable types are not statically assigned; they can change as values are reassigned.
Importance of Naming Variables
Variable names should:
Be unambiguous.
Clearly indicate the purpose of the variable.
Be understandable to others reading the code.
Conform to
snake_case
convention in Python.Avoid reserved words (keywords) in Python.
Examples of Bad Variable Names
pikachu = "Carlota"
potato = 2025
x = "20/05/2028"
PI = 3.546
True = False
global = "The world"
Person name = "Laura"
PersonName = "Lucia"
Examples of Good Variable Names
user = "Carlota"
year = 2025
graduation_date = "20/05/2028"
new_pi_number = 3.546
new_true = False
global_variable = "The world"
person_name = "Laura"
Namespaces in Python
Definition and Types of Namespaces
A namespace is a collection of defined symbolic names along with the object information each name references.
It functions like a dictionary, with keys as object names and values as the objects.
Four types of namespaces in Python:
Built-In
Global
Enclosing
Local
Variable Scope and LEGB Rule
Python resolves variable references using a defined order:
Local: Looks in the local function's scope.
Enclosing: Checks the enclosing function's scope if it doesn’t find it locally.
Global: Searches in the global scope.
Built-in: Finally looks in the built-in namespace.
Examples of Variable Scope
Local scope example:
def example(): variable_name = "LEGB rule" print(variable_name) example() print(variable_name)
Enclosing scope example:
def example(): first_variable = "outer function" def example_inner(): second_variable = "inner function" print(f"{first_variable} // {second_variable}") print(first_variable) example_inner() example()
Global scope example:
variable_name="global function" def example(): def example_inner(): print(variable_name + " inner") print(variable_name + " outer") example_inner()
Built-in scope example:
from math import pi def example(): def example_inner(): print(pi) example_inner() print(pi)
Conditional Statements in Python
Structure of Conditional Statements
Conditional statements in Python can be structured using three main forms:
if [condition]
elif [condition]
else
The
if
andelif
statements are utilized when conditions are required, whileelse
is used for all other cases that do not meet previous conditions.
Exercise Examples for User Interaction
Program prompts for user’s name and responses based on given conditions, illustrating how to construct basic programs using conditional logic:
Responds differently based on user input (e.g. if the name is not Jerry).
Asks for integers and gives feedback based on their value.
Iterations in Python
Types of Loops
Iterations can be performed using:
While loop: Executes as long as a stated condition is true.
For loop: Iterates over a data structure (like a list, tuple, dictionary, etc.).
Key Statements for Loops
Break: Stops the loop's execution.
Continue: Skips to the next iteration of the loop.
Range: Generates a sequence of numbers.
Exercises Examples
Iteration examples that involve user inputs, such as repeatedly asking for numbers while managing conditions and responses, including calculations like factorial.