Skip to content
تعلیم کہانی
  • Home
  • Courses
  • Blog Articles
  • Login
  • Toggle website search
Menu Close
  • Home
  • Courses
  • Blog Articles
  • Login
  • Toggle website search

Introduction to Python

  1. Home>
  2. Courses>
  3. Introduction to Python
  • Home
  • Courses
  • Freelancing
  • Introduction to Python

Introduction to Python

Curriculum

  • 2 Sections
  • 26 Lessons
  • 10 Weeks
Expand all sectionsCollapse all sections
  • Week 1
    10
    • 1.1
      Introduction to Python – Part A
      15 Minutes
    • 1.2
      Introduction to Python – Part B
      12 Minutes
    • 1.3
      Setting Up Visual Studio Code for Python Programming and Debugging
      6 Minutes
    • 1.4
      Running Your First Program in Python and Debugging
      9 Minutes
    • 1.5
      Taking Input from the User and Utilizing it
      8 Minutes
    • 1.6
      Cleaning User Input Using ‘Methods’ of Strings
      6 Minutes
    • 1.7
      Good Coding Practices
      14 Minutes
    • 1.8
      Positional and Named Parameters in Python
      9 Minutes
    • 1.9
      Joining String Together
      4 Minutes
    • 1.10
      Assignments for Week 1
      60 Minutes
  • Week 2
    16
    • 2.0
      Manipulating Strings in Python
      14 Minutes
    • 2.1
      Splitting Text and String Expressions in Python
      6 Minutes
    • 2.2
      Data Types for Numbers in Python
      14 Minutes
    • 2.3
      Mathematical Expressions and Operators in Python
      9 Minutes
    • 2.4
      Defining a Custom Function in Python
      14 Minutes
    • 2.5
      How to Get a Return Value from a Function in Python
      12 Minutes
    • 2.6
      Conditional Statements – If, Elif and Else in Python
      10 Minutes
    • 2.7
      Multiple Comparisons using If
      9 Minutes
    • 2.8
      Determine if a Number is Odd or Even in Python
      9 Minutes
    • 2.9
      Match – Case in Python
      8 Minutes
    • 2.10
      Assignments for Week 2
      90 Minutes
    • 2.11
      While Loop In Python
    • 2.12
      For Loops in Python
    • 2.13
      Print Text Multiple Times in Python
    • 2.14
      Infinite Loop Using While, Continue and Break Statements
    • 2.15
      Detailed Look at Lists in Python

Assignments for Week 2

Assignment 1

In a popular TV show, a bank has a policy to give customers $100 if their staff fails to greet them with “hello.” A character named Kramer is greeted with “hey” instead of “hello,” prompting him to claim the $100. The bank’s manager offers a compromise, saying, “You received a greeting starting with ‘h’, how about $20?” Kramer agrees to the offer.

SEINFELD Bank Retention Pledge Must Say Hello 100 Dollars.wmv

Create a Python program that checks the greeting provided by the user and determines the amount of money they would receive based on the following rules.

Implement a program in a file called bank.py that prompts the user for a greeting and outputs an amount of money based on the following criteria:

  1. If the greeting starts with “hello”, output $0.
  2. If the greeting starts with an “h” (but not “hello”), output $20.
  3. Otherwise, output $100.

Hints

  • As you know, a str comes with different methods, per docs.python.org/3/library/stdtypes.html#string-methods.
  • Be sure to give $0 not only for “hello” but also “hello there”, “hello, Newman”, and anything like that.

 

The output of your program should look like this:

 


 

Assignment 2

Although Windows and macOS sometimes conceal them, most files have extensions, which are suffixes starting with a period (.) at the end of their names. For example, the file names for GIFs end in .gif, and the file names for JPEGs end in .jpg or .jpeg. When you double-click a file to open it, your computer uses its extension to decide which program to launch.

In contrast, web browsers use media types (previously known as MIME types) to determine how to display files found on the web. When you download a file from a web server, the server sends an HTTP header with the file, indicating its media type. For instance, the media type for a GIF is image/gif, and for a JPEG, it is image/jpeg. To identify the media type of a file, a web server usually looks at the file’s extension and maps it accordingly.

Refer to Common MIME types on MDN Web Docs for a list of common types.

In a file called extensions.py, create a program that prompts the user for a file name and then outputs the file’s media type if the name ends, case-insensitively, with any of these suffixes:

  • .gif
  • .jpg
  • .jpeg
  • .png
  • .pdf
  • .txt
  • .zip

If the file name ends with a different suffix or has no suffix at all, output application/octet-stream as a default.

The output of your program should look like this:


Assignment 3

Python already supports various mathematical operations such as addition, subtraction, multiplication, and division. However, let’s create a program that allows users to perform these operations without needing to know Python.

In a file called interpreter.py, write a program that prompts the user for an arithmetic expression and then calculates and displays the result as a floating-point number formatted to one decimal place. Assume the user’s input will be formatted as x y z, with one space between each element, where:

  • x is an integer
  • y is one of the operators: +, -, *, or /
  • z is an integer

For example, if the user inputs 1 + 1, your program should output 2.0. You can assume that if y is /, z will not be 0.

Just as Python itself acts as an interpreter for Python code, your interpreter.py will act as an interpreter for basic arithmetic expressions.

Hints:

Recall that a string (str) in Python has various methods, as detailed in the official documentation, including split, which divides a string into a list of substrings. For example, if expression is a string like 1 + 1, then:

x, y, z = expression.split(" ")

will assign 1 to x, + to y, and 1 to z.

The output of your code should look like this:

 


 

Assignment 4

Imagine you are in a country where it’s customary to eat breakfast between 7:00 and 8:00, lunch between 12:00 and 13:00, and dinner between 18:00 and 19:00. Wouldn’t it be useful to have a program that tells you what meal to eat and when?

In meal.py, create a program that prompts the user for a time and outputs whether it’s time for breakfast, lunch, or dinner. If it’s not time for a meal, the program should not output anything. Assume the user’s input will be in 24-hour format as #:## or ##:##. Also, assume that each meal’s time range is inclusive. For example, if the time is 7:00, 7:01, 7:59, or 8:00, or any time in between, it’s breakfast time.

Structure your program as shown below. The convert function should be called by main and will convert the time, provided as a string in 24-hour format, into the corresponding number of hours as a float. For example, if given “7:30” (i.e., 7 hours and 30 minutes), convert should return 7.5 (i.e., 7.5 hours).

def main():
    ...


def convert(time):
    ...


if __name__ == "__main__":
    main()

Hints

  • Recall that a str comes with quite a few methods, per docs.python.org/3/library/stdtypes.html#string-methods, including split, which separates a str into a sequence of values, all of which can be assigned to variables at once. For instance, if time is a str like "7:30", then hours, minutes = time.split(":") will assign “7” to hours and “30” to minutes.
  • Keep in mind that there are 60 minutes in 1 hour.

The output of your program should look like the following:

Match – Case in Python
Prev
While Loop In Python
Next
Copyright 2025 - TaleemKahani.com

Modal title

Main Content