Learning Python

We will be doing this lab inconjuction with the Python lecture. Key concepts will be explained and you will put into practice.

0. Interpreter for Python

  • We should not need to do anything for the university machines to run python as this is already part of the PATH, you can run the following commands:

    Terminal

    $ which python
    $ python --version
    $ which pip
    $ pip --version
    

    Output

So let's make our first program.

Exercises 1. Hello World

  1. Frist we need to create a directory and file, call directory learning_python and create a file in side it called helloworld.py:

  2. Modify the files content to contain the following:

    Code

    def main():
        print("Hello World!")
        print("Goodbye World!")
    
    if __name__=="__main__":
        main()
    

Exercise 2: Variables and Data Types

Instructions:

  1. Create a new file called variables.py

  2. Create a variable called name and assign it your name as a string.

  3. Create a variable called age and assign it your age as an integer.

  4. Create a variable called height and assign it your height in meters as a float.

  5. Print each variable on a new line.

  6. and then run it.

    Code

    name = "Your name"
    age = 25
    height = 1.75
    
    print(name)
    print(age)
    print(height)
    

    Terminal

    $ python variables.py
    > Your name
    > 25
    > 1.75
    

Exercise 3: Basic Operations

Instructions:

  1. Create a new file call it basicMaths.py

  2. Create two variables, a and b, and assign them any two numbers.

  3. Perform and print the results of addition, subtraction, multiplication, and division between a and b.

  4. Use the modulus operator to find the remainder of a divided by b and print the result.

  5. and then run it.

    Code

    a = 10
    b = 3
    
    print("Addition:", a + b)
    print("Subtraction:", a - b)
    print("Multiplication:", a * b)
    print("Division:", a / b)
    print("Modulus:", a % b)
    

    Terminal

    $ python basicMaths.py
    > Addition: 13
    > Subtraction: 7
    > Multiplication: 30
    > Division: 3.3333333333333335
    > Modulus: 1
    

Exercise 4: Lists and Indexing

Instructions:

  1. Create a new file called lists.py

  2. Create a list called fruits that contains the following fruits: "apple", "banana", "cherry".

  3. Print the first fruit in the list.

  4. Add a new fruit "orange" to the list and print the updated list.

  5. Remove "banana" from the list and print the list again.

  6. and then run it

    Code

    fruits = ["apple", "banana", "cherry"]
    print("First fruit:", fruits[0])
    
    fruits.append("orange")
    print("Updated list:", fruits)
    
    fruits.remove("banana")
    print("List after removing banana:", fruits)
    

    Terminal

    $ python lists.py
    > First fruit: apple
    > Updated list: ['apple', 'banana', 'cherry', 'orange']
    > List after removing banana: ['apple', 'cherry', 'orange']
    

Exercise 5: Conditional Statements

Instructions:

  1. Create a file called conditionals.py

  2. Write a program that checks if a number is positive, negative, or zero.

  3. Assign a value to the variable number and use if, elif, and else to print:

    • "Positive" if the number is greater than 0
    • "Negative" if the number is less than 0
    • "Zero" if the number is exactly 0.
  4. and then run it.

  5. change 5 to 0 and run again

    Code

    number = 5
    
    if number > 0:
        print("Positive")
    elif number < 0:
        print("Negative")
    else:
        print("Zero")
    

    Terminal

    python conditionals.py
    Positive
    python conditionals.py
    Zero
    

Exercise 6: Loops

Instructions:

  1. Create a file called loops.py

  2. Write a for loop that prints numbers from 1 to 5.

  3. Create a list of your favorite colors and use a for loop to print each color.

  4. Write a while loop that prints numbers from 10 down to 1.

  5. run the program

    Code

    # For loop to print numbers from 1 to 5
    for i in range(1, 6):
        print(i)
    
    # For loop to print each favorite color
    favorite_colors = ["blue", "green", "red"]
    for color in favorite_colors:
        print(color)
    
    # While loop to print numbers from 10 down to 1
    n = 10
    while n > 0:
        print(n)
        n -= 1
    

    Terminal

    $ python loops.py
    > 1
    > 2
    > 3
    > 4
    > 5
    > blue
    > green
    > red
    > 10
    > 9
    > 8
    > 7
    > 6
    > 5
    > 4
    > 3
    > 2
    > 1
    

Exercise 7: Functions

Instructions:

  1. Create a new file called functions.py

  2. Write a function called greet that takes a name as a parameter and prints "Hello, <name>!".

  3. Write a function square that takes a number as a parameter and returns its square.

  4. Test both functions by calling them with sample inputs.

    Code

    def greet(name):
        print(f"Hello, {name}!")
    
    def square(number):
        return number ** 2
    
    # Testing the functions
    greet("Alice")
    print("Square of 4:", square(4))
    

    Terminal

    $ python functions.py
    > Hello, Alice!
    > Square of 4: 16
    

Exercise 8: Dictionaries

Instructions:

  1. Create the a file called dictionaries.py

  2. Create a dictionary called student with keys: name, age, and courses. Assign appropriate values to each.

  3. Print the student's name and age.

  4. Add a new key graduation_year with a sample year and print the updated dictionary.

  5. and run it.

    Code

    student = {
        "name": "Your name",
        "age": 21,
        "courses": ["Subject1", "Subject2"]
    }
    
    print("Name:", student["name"])
    print("Age:", student["age"])
    
    student["graduation_year"] = 2025
    print("Updated student dictionary:", student)
    

    Terminal

    $ python dictionaries.py
    > Name: Your name
    > Age: 21
    > Updated student dictionary: {'name': 'Your name', 'age': 21, 'courses': ['Subject1', 'Subject2'], 'graduation_year': 2025}