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 = 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.

Importance of Variable Names

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 of Bad Variable Names

  • Examples include:

    • pikachu = "Carlota"

    • potato = 2025

    • True = False

    • global = "The world"

    • Person name = "Laura"

Good Variable Names

  • 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"

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:

    1. Local: Searches the innermost local scope of the function.

    2. Enclosing: Searches the enclosing function scopes.

    3. Global: Searches the global scope.

    4. Built-in: If the variable is not found, it checks built-in names.

Conditional Statements

  • Conditional statements in Python:

    • If [condition]

    • Elif [condition]

    • Else

  • If and elif require conditions to execute their blocks, whereas else executes 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

  1. Create a program to prompt the user for their name and total portions with specified conditions for calculating cost (5.90 per portion).

  2. Write a program to determine magnitudes of user-entered integers.

  3. Develop a calculator that executes specified operations based on user input.

  4. Implement a temperature conversion program from Fahrenheit to Celsius.

  5. Ask for hourly wage and calculate daily wages while considering Sundays.

  6. Suggest clothing based on weather conditions.

  7. 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.

robot