Hopp til hovedinnhold

Exercices for Python Basics

Exercise Set 1: Comments and Basic Printing

  1. First Steps with Comments Write a program that includes:

    • A comment with your name
    • A comment explaining what the program does
    • A print() statement that displays "Hello, World!"
  2. Comment Practice Look at the following code and add some comments explaining what each line does:

    name = "Alice"
    age = 25
    print(name)
    print(age)

Exercise Set 2: Basic Operations

  1. Calculator Practice Create a program that:

    • Creates two variables with different numbers
    • Performs all basic arithmetic operations (+, -, *, /) between them
    • Prints each result
    • Includes comments explaining each operation
  2. String Operations Write a program that:

    • Creates two variables containing your first and last name
    • Concatenates them with a space in between
    • Prints the full name
    • Prints the length of your full name

Exercise Set 3: Variables and Types

  1. Type Explorer Create variables of different types and use type() to explore them:

    • Create an integer variable
    • Create a float variable
    • Create a string variable
    • Create a boolean variable
    • Print the type of each variable
  2. Variable Transformation Write a program that:

    • Creates a number variable with the value 42
    • Converts it to a string and stores it in a new variable
    • Prints both variables and their types

Exercise Set 4: Functions

  1. Simple Function Creation Create a function that:

    • Takes no parameters
    • Prints "Good morning!" when called
    • Call the function three times
  2. Basic Calculator Function Write a function that:

    • Takes two numbers as parameters
    • Prints their sum
    • Prints their difference
    • Prints their product
    • Call the function with different numbers

Exercise Set 5: Indentation Practice

  1. Fix the Indentation Fix the indentation in the following code:

    def greet(name):
    print("Hello")
    print(name)
    print("How are you?")
  2. Create Nested Code Write a program that:

    • Creates a function containing an if statement
    • Uses proper indentation
    • Has at least three levels of indentation

Challenge Exercises

  1. Mini Calculator Program Create a program that:

    • Defines separate functions for addition, subtraction, multiplication, and division
    • Takes two numbers as input
    • Performs all operations
    • Includes appropriate comments
    • Uses proper indentation
    • Prints results in a formatted way
  2. Type Converter Write a program that:

    • Creates variables of different types (int, float, string)
    • Converts between these types where possible
    • Includes error handling comments for impossible conversions
    • Prints the original and converted values with their types

Tips for Completing These Exercises:

  • Try to solve each exercise without looking at solutions first
  • Test your code with different inputs
  • Pay attention to proper indentation
  • Add comments to explain your code
  • If you get stuck, break the problem into smaller parts
  • Remember to use meaningful variable names