0% found this document useful (0 votes)
3 views

ITC Lab 09

This document is a lab guide for getting started with Python, covering installation, basic syntax, input/output, data types, variables, and conditional statements. It includes step-by-step instructions for setting up Python and Visual Studio Code, along with examples of Python programs and various lab tasks for practice. The tasks range from simple programs like checking even or odd numbers to more complex applications like a banking system and a geometry calculator.

Uploaded by

sanamre143ucp
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

ITC Lab 09

This document is a lab guide for getting started with Python, covering installation, basic syntax, input/output, data types, variables, and conditional statements. It includes step-by-step instructions for setting up Python and Visual Studio Code, along with examples of Python programs and various lab tasks for practice. The tasks range from simple programs like checking even or odd numbers to more complex applications like a banking system and a geometry calculator.

Uploaded by

sanamre143ucp
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Introduction to Computing

Lab 09
Topic Getting Started with Python
 Tool Installation
 Basic Python Syntax
Objective  Input/Output
 Datatypes and Variables
 Conditional Statements
Installation Guide:
Python is free. To download Python to your
On your computer, you must first visit the official website at http://python.org

Download and then custom install the Python setup check all the boxes while
installing.
Write “cmd” in the start then open the command prompt, and check if Python
is installed.
Then write the command python --version.
Once you have Python successfully downloaded, there are several integrated
development environment (IDE) for Python that you can write and run your
Python programs. We’ll use VS Code.
An IDE allows you to create, edit, run, troubleshoot, save, and open your code
all in the same easy-to-use program.

To download the setup use the link: Download Visual Studio Code - Mac, Linux,
Windows
After installing the IDE. Launch the VS Code
Download extension for VS code

Click on extension button and search Python in search bar. Install the first
suggestion that appears for python:
After installation of python extension, go to the explorer option and click on
open folder

Create a folder in any drive with any name you like and select that folder in the
explorer
Click on new file and create a file having any name with (.py) extension. Double
click or press enter to open the editor.

Write your first program with a output statement:


print(“hello world!”)
Then press run button to see the output in terminal
Here are some statements that you need to remember:
Comments: #, “
Examples:
#First Program -> Single line comment
“”” -> multiple line comment
My First Program
Of
Python
“””
Output: print()
Examples:
1. print(“First Program”)

2. num = 10
print(“Number: ”,num)

3. num = 10, num2 = 45


print(“Number 1: ”,num , “\nNumber 2: ”, num2)

Input: input()
Examples:
1. var = input() -> by default string type
2. var = int(input()) -> convert to int to use the variable
for numbers

3. print(“Enter Number: ”)
num=input()
print(“Number: ”,num)

4. num=input(“Enter Number: ”)
print(“Number: ”,num)

5. num1=int(input(“Enter Number 1: ”))


num2=int(input(“Enter Number 2: ”))
sum = num1 + num2
print(num1, “ + ”, num2, “ = ”, sum)

Student Application Example:


name= input("Enter Name: ")
age= int(input("Enter Age: "))
email= input("Enter Email: ")
degree= input("Enter Degree: ")
contact= input("Enter Contact: ")
marks= int(input("Enter Marks out of 50: "))

print("Name: ", name,


"\nAge: ",age,
"\nEmail: ",email,
"\nContact: ",contact,
"\nDegree: ",degree)

percentage=(marks/50)*100
print("Marks: ",marks,
"\nPercentage: ",percentage,"%")

Output:
Enter Name: ahmad
Enter Age: 20
Enter Email: [email protected]
Enter Degree: bsse
Enter Contact: 0333-1234567
Enter Marks out of 50: 42
Name: ahmad
Age: 20
Email: [email protected]
Contact: 0333-1234567
Degree: bsse
Marks: 42
Percentage: 84.0 %

Conditional Statements:
1. if/else
2. if/elif/else

Grade Example using if:


name= input("Enter Name: ")
marks= int(input("Enter Marks out of 100: "))
grade = ''
if marks>=85:
grade = 'A'
if marks>=70 and marks<85:
grade = 'B'
if marks>=60 and marks<70:
grade = 'C'
if marks>=50 and marks<60:
grade = 'D'
if marks<50:
grade = 'F'

print("Grade: ",grade)

Output:
Enter Name: ali
Enter Marks out of 100: 76
Grade: B

Grade Example using if/elif:


name= input("Enter Name: ")
marks= int(input("Enter Marks out of 100: "))
grade = ''
if marks>=85:
grade = 'A'
elif marks>=70:
grade = 'B'
elif marks>=60:
grade = 'C'
elif marks>=50:
grade = 'D'
else:
grade = 'F'

print("Grade: ",grade)

Output:
Enter Name: ali
Enter Marks out of 100: 76
Grade: B
Lab task
Task 1: Even or Odd
Write a Python program to check if a number is even or odd.

Task 2: Event Ticket Price Calculator

Write a Python program for designing a system for an event ticket booth. The system
should:

1. Ask the user to input the number of adults and children attending the event.
2. The price per adult is $20, and the price per child is $10.
3. Calculate and display the total cost of the tickets.

Input/Output Steps:

1. Input: Number of adults and children.


2. Output: Total cost for the tickets.

Task 3: Classroom Average Marks Calculator

A teacher wants to calculate the average marks for a class. The system should:

1. Ask the user to input the number of students.


2. For each student, input their marks out of 100.
3. Calculate and display the average marks for the class.

Input/Output Steps:
1. Input: Number of students and each student’s grade.
2. Output: Average marks.

Task 4: Currency Converter (USD to PKR)

You are building a currency converter for a travel website. The system should:

1. Ask the user to input the amount of money they want to convert.
2. Based on the exchange rate, calculate the converted amount.
3. Display the converted amount.

Input/Output Steps:

1. Input: source currency and target currency.


2. Output: Converted amount based on exchange rate.
Task 5: Candidate Criteria
A company hires male and female employees based on their age, gender, and marital status
for their different franchises in big cities and small towns. Write a Python program that
takes input of age, gender, and marital status of a candidate to decide which franchise is
suitable for him/her according to the criteria given below:

 If the candidate is female, unmarried, and aged between 18 and 35, she is suitable for
both cities and towns; otherwise, she will only work in big cities.
 If the candidate is male, unmarried, and between 18 and 42 years old, then he is
suitable for both cities and towns.
 if the candidate is male and married and their age is between 18 to 42 then he is
suitable for big cities only.
 And any other input of age should print "Candidate is not preferred for the job".

Task 6: Banking System


A bank in your town updates its customers’ accounts at the end of each month. The bank
offers two types of accounts: savings and checking. Every customer must maintain a
minimum balance. If a customer’s balance falls below the minimum balance, there is a
service charge of $10.00 for savings accounts and $25.00 for checking accounts. If the
balance at the end of the month is at least the minimum balance, the account receives
interest as follows:
 Savings accounts receive 4% interest.
 Checking accounts with balances of up to $5,000 more than the minimum balance
receives 3% interest; otherwise, the interest is 5%.
Write a python program that reads a customer’s account number, account type, minimum
balance that the account should maintain, and current balance. The flowchart should then
output the account number, account type, current balance, and an appropriate message.
Test your program by running it five times one by one, using the following data:
46728 saving 1000 2700
87324 checking 1500 7689
79873 saving 1000 800
89832 checking 2000 3000
98322 checking 1000 750

Task 7: Water Heater Energy Usage

You are designing a water heater controller that adjusts based on water temperature. The system
should:

If the water temperature is below 40°C, display: "Heating water."


If the water temperature is between 40°C and 60°C, display: "Water temperature is optimal."
If the water temperature is above 60°C, display: "Cooling water."
Steps for if-else Logic:
Input the current water temperature.
Use if-else conditions to determine whether to heat, cool, or maintain the water temperature.

Task 8: Ride Sharing

You are building a ride-share app that adjusts the price of a ride based on traffic conditions
and time of day. The system should:

1. If it’s rush hour (7-9 AM or 5-7 PM) and heavy traffic, increase the base fare by 50%.
2. If it’s rush hour but traffic is light, increase the base fare by 20%.
3. Use the normal base fare if it’s not rush hour.

Steps for if-else Logic:

1. Input the time of day and traffic conditions (heavy or light).


2. Use if-else conditions to adjust the fare based on the time and traffic conditions.

Task 9: Calorie Tracker


You are building a simple calorie tracker. The user enters the type of meal (e.g., "breakfast",
"lunch", "dinner", "snack") and the number of calories consumed. Track the calories on the
given conditions:
 If it's breakfast and calories are more than 500, print "High calorie breakfast."
 If it's lunch and calories are below 300, print "Low calorie lunch."
 If it's dinner and calories are between 400 and 700, print "Dinner within
recommended calorie range."
 For snacks with more than 200 calories, print "High calorie snack."
 Otherwise, print "Calorie intake noted.

Home Task
Task 10: Geometry Calculator
Write a Python program to create a geometry calculator:
Display Menu:
 Calculate the Area of a Circle
 Calculate the Area of a Rectangle
 Calculate the Area of a Triangle
 Quit
Enter your choice (1-4):
If the user enters 1, the program should ask for the radius of the circle and then display its
area. Use the following formula:
area = πr2
Use 3.14159 for π and the radius of the circle for r. If the user enters 2, the program should
ask for the length and width of the rectangle and then display the rectangle’s area. Use the
following formula:
area = length * width
If the user enters 3 the program should ask for the length of the triangle’s base and its
height, and then display its area. Use the following formula:
area = base * height * 0.5
If the user enters 4, the program should end.

Input Validation: Display an error message if the user enters a number outside the range of 1
through 4 when selecting an item from the menu. Do not accept negative values for the
circle’s radius, the rectangle’s length or width, or the triangle’s base or height.

You might also like