Python Basics Homework

Welcome to your Python Basics Homework! This notebook is designed to help you practice and improve your Python programming skills. Please follow the instructions carefully and complete the tasks below.

Instructions

  • Read each problem statement carefully.
  • Write the Python code to solve each problem in the designated cell.
  • Do not delete or modify any pre-existing code or comments unless instructed to do so.
  • Run your code to ensure it works as expected and you have solved the problem correctly.
  • Save your notebook once you have completed the homework.

Problems

This homework consists of problems that cover basic Python operations, functions, and libraries. You will practice using basic operations, creating functions, working with lists and strings, and using the random and pandas libraries.

Let’s get started!

Problem 1: Basic Operations and Functions

Part 1.1: Basic Operations

Perform the following operations and print the results:

  1. Add 15 and 23.
  2. Subtract 50 from 200.
  3. Multiply 25 by 4.
  4. Divide 100 by 3 (get the result as a float number).

Part 1.2: Creating and Using Functions

Create a function named calculate_area that takes the base and height of a triangle as parameters and returns its area. The formula to calculate the area of a triangle is (base * height) / 2.

Then, call this function with base=10 and height=5 and print the result.

Problem 2: Working with Lists and Strings

Part 2.1: Lists

You have a list of numbers: numbers = [10, 20, 30, 40, 50].

  1. Append the number 60 to the list.
  2. Insert the number 25 between 20 and 30.
  3. Remove the number 40 from the list.
  4. Print the modified list.

Part 2.2: Strings

You have a string: sentence = 'Python is fun!'.

  1. Convert the string to uppercase and print it.
  2. Count and print the number of ‘n’ characters in the string.
  3. Replace ‘fun’ with ‘awesome’ in the string and print the result.

Problem 3: Using Libraries

Part 3.1: Importing Libraries and Using the random Library

  1. Import the random library.
  2. Generate and print a random integer between 1 and 100 using the random.randint() function.
  3. Generate and print a random float number between 0 and 1 using the random.random() function.

Part 3.2: Introduction to the pandas Library

  1. Import the pandas library with the alias pd.
  2. Create a dictionary with two keys: ‘Name’ and ‘Age’. The ‘Name’ key should have a list of names as values, and the ‘Age’ key should have a list of ages as values.
  3. Create a pandas DataFrame from the dictionary and print it.
  4. Print the average age from the ‘Age’ column in the DataFrame.