Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
48 views

Practical 1 - Basics of Python

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

Practical 1 - Basics of Python

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

Basics of Python Programming

Basics of python
programming
Practical 1

Prepared by Pema Wangdi

What you will learn / do in this lab

1. Perform input and output

2. Working with import function

3. Work with basic data types like integers, floats and strings

4. Use assignment, arithmetic, comparison and logical operators


Table of contents
Table of contents..............................................................................................................
1. Input, output, variables in python................................................................................
Task 1: Converting numeric inputs...............................................................................
Task 2: Formatting outputs..........................................................................................
Task 3 : Average of electricity bill................................................................................
2. Using import in python.................................................................................................
Task 1: Timer...............................................................................................................
Task 2: Reaction game.................................................................................................
Task 3: Days before assignment is due........................................................................
3. Working with Python numeric types.............................................................................
Task 1: Perform basic mathematical functions............................................................
Task 2: Mathematical functions in python....................................................................
4. Working with Python string types.................................................................................
Task 1: Basics of strings...............................................................................................
Task 2: Performing simple string operations................................................................

1. Input, output, variables in python


Task 1: Converting numeric inputs
Write a Python program that prompts the user to enter the length of a square and prints out
the area and perimeter of the square as shown in the figure below.

Hint -- > Remember to convert your inputs to data type int or float before performing
calculations!

2
Task 2: Formatting outputs
Write a Python program that prompts the user to enter his height and weight, then calculates his
BMI to a precision of 1 decimal place as shown in the figure below. Also check under which
category the person falls into: underweight, healthy, overweight or obesity.

Hint -- > Remember to convert your inputs to data type float before performing calculations!

BMI = (weight (in kg)/height(in cm)/height (in cm))*10000

*** To Do: HOME WORK

Task 3 : Average of electricity bill


Write a Python program that prompts the user to enter his electricity bill for the last 6 months
and prints out the average of his electricity bills as shown below.

Hint -- > Remember to convert your inputs to data type float before performing calculations!

3
2. Using import in python
As mentioned earlier, Python programs can do almost anything.

Why is Python so “powerful”?

The reason is simple – there are many other Python programmers out there, and they have
written a lot of Python code which you can just re-use by “importing” their code, also known as
“modules” into your own Python program.

In this section, you will learn how to import Python modules that can help you complete certain
tasks you may need to do.

For example, you may need your Python program to wait for 2 seconds before executing the next
action. In that case, you can import the time module, which will allow you to make your Python
programme “sleep” for a specified number of seconds.

Perhaps you need to generate a list of 10 random numbers in the range of 1 to 100. To do that,
you can import the random module, which will allow you to generate random numbers of your
preferred range.

Task 1: Timer
Write a Python program that acts like a count-down timer.

Your program should:

1. prompt the user to enter the number of seconds he wishes to count down and store it in a
variable called time_to_wait
2. use the sleep function to wait for the specified number of seconds
3. display a message “Time is up!” when the specified number of seconds has elapsed

Expected output

4
Task 2: Reaction game

Write a Python ‘game’ that tests how fast a user can respond.

Your program should:

1. store the current time in a variable time_now


2. Prompt the user with a message “Timer starts now! Press any key as quickly as possible!”
and wait for the user to key in his input in a variable called user_input
3. Record and store the time at which the user’s input was captured in a variable
time_entered
4. Calculate the time that has elapsed between time_now and time_entered, which is the
time the user took to respond. Store this in a variable time_response
5. Display time_response to the user as shown in the diagram below

Expected result

*** To Do: HOME WORK

Task 3: Days before assignment is due


Write a Python program that prompts the user for the due date of an assignment, then calculates
the number of days till the due date of the assignment from the current date.

Your program should:

1. store the current date in a variable date_today


2. Prompt the user with a message “Please enter the due date of the assignment”
3. Store the user’s input in a variable named assignment_duedate
4. Calculate the number of months and days between date_today and assignment_duedate,
and store the result in the variable number_of_months and number_of_days
5. Display the result to the user as shown in the diagram below

Hint: Refer to Using import example in Powerpoint slides

5
Expected result

3. Working with Python numeric types


Task 1: Perform basic mathematical functions
Write a Python program that performs the fundamental operations in mathematics: addition,
subtraction, multiplication and division.

Your program should:


1. Prompt the user to enter four numbers and store them in the variables n1,n2,n3 and n4
2. Add up the four numbers and store the calculated value in the variable add
3. Subtract n4 from n1 and store the calculated value in the variable difference
4. Multiply n2 by n3 and store the calculated value in the variable product
5. Divide the sum of n1 and n2 by the sum of n3 and n4, and store the calculated value in
the variable quotient
6. Find the remainder of the product of n1 and n2, divided by the product of n3 and n4 and
store in the value remainder
7. Calculate n1 to the power of n2 and store in the variable power
8. Print out the outputs as shown in the diagram below

6
*** To Do: HOME WORK

Task 2: Mathematical functions in python


Write a Python program that uses mathematical functions in Python like sqrt, factorial, ceil, floor
and so on.

Your program should:

1. Prompt the user to enter four numbers and store them in the variables n1,n2,n3 and n4
2. Perform the following operations on each variable as specified below.
3. Calculate the square root of n1 and store the value of the result to a variable
n1_squareroot. Print out the ceiling value of n1_squareroot.
4. Multiply n2 by the mathematical value of pi and store the result to a variable
n2_times_pi. . Print out the truncated value of n2_times_pi.
5. Calculate the value of n3 to the power of n1 and store the result to n3_powerof_n1. Print
out the floor value of n3_powerof_n1.
6. Compute the factorial of n4 and store the result to a variable n4_factorial. Divide
n4_factorial by n3 and store the result to n4n3. Compute the fractional and integer parts
of n4n3 and print out their values.

Hint --> Remember to convert your inputs to data type float before performing calculations!

Your program should produce outputs similar to that below.

7
4. Working with Python string types
Task 1: Basics of strings

1. Define a 'scientist' variable set to the string 'albert Einstein'. Invoke the title method on
the string variable. Assign the returned string to a 'proper_name' variable.

2. The 'wasteful_string' below has a lot of useless whitespace. Invoke the correct method on
wasteful_string to clear ALL whitespace (beginning and end). Assign the returned string
from the correct method to an 'unwasteful_string' variable

wasteful_string = " 9:00PM "

3. The party_attendees string below contains a list of people attending our party. Use the 'in'
operator to determine if "Karma" is attending the party. Assign the resulting Boolean to
an 'is_attending' variable

8
party_attendees = "Dorji, Yeshi, Karma, Tshering"

4. Declare a cleanup function that accepts a single string input. The function should

a. Remove all leading and trailing whitespace from the input string
b. Capitalize the first letter of the input string
c. Return the new string
EXAMPLES:

cleanup(" hello ") => "Hello"


cleanup(" see you tomorrow ") => "See you tomorrow"

*** To Do: HOME WORK

Task 2: Performing simple string operations


Write a Python program to achieve the following:

1. Prompt the user to enter 3 random strings of at least 8 characters long, each separated by
a comma

A sample of what the user might enter is given below

When Breath Becomes Air, The 91-Storey Treehouse, Harry Potter and the Prisoner of
Azkaban

2. Use the split() function in Python to separate the strings and store the separated strings
into three variables, s1, s2 and s3

3. Perform the following operations on each string:


a. Calculate the length of each string
b. Extract the 2nd and third characters of s1, fifth to seven characters of s2 and last
two characters of s3
c. Your output should look similar to that as shown below.

9
**********************************THE END*******************************

10

You might also like