Practical 1 - Basics of Python
Practical 1 - Basics of Python
Basics of python
programming
Practical 1
3. Work with basic data types like integers, floats and strings
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!
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.
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.
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.
Expected result
5
Expected result
6
*** To Do: HOME WORK
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!
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
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:
1. Prompt the user to enter 3 random strings of at least 8 characters long, each separated by
a comma
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
9
**********************************THE END*******************************
10