Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Lab Sheet – 3.

Python -1 Problem Solving and Algorithmic Thinking


__________________________________________________________________________________

Topics covered. Input and output in python, operators, precedence and associativity of
operators, simple expressions, basic built-in functions
Input and Output in python
The standard input output functions in python are
Input() for input operation and
print() for output operation.
They are called standard i/o functions because they communicate with the standard i/o
devices. That is, input() function reads input data from the standard input device – keyword
and the print() function shows output data on the standard output device – monitor.

1. Write a python program to print “Hello World”.


2. Write a program to read a name and print it.
3. Write a program to read an integer and print its square. Note: Python input() function
returns input values as strings. So numeric inputs need typecast, See the example below.

4. Place holders in print function. While formatting the output in print() function, we can use
place holders. Place holder is a combination of characters in python that begins with %
character. They show the place in the format string where a value is to be inserted. See the
following examples.

The %d is replaced with the data given. The string “%d + %d = %d” in the first example and
the string “The square of the number %d is %d” in the second example are called format
strings.
5. Place holders can be used to specify the precision. Consider the following examples.
In the first example the place holder %2d specifies that the integer that is to be printed at
its place in the format string must be of 2 characters length (i.e. two-digit number). So, 23
is printed and .4 is ignored
In the second example the value must be a 3-digit number. But since the original number
has only 2 digits, the output is padded with one leading blank letter.
In case of float place holders, %a.b means there are a characters in total, out of which b are
decimal points. (b precision)
6. Write a python program to read two integers and print their product with a suitable
message.
7. Print the value of pi with 5 places of precision.
There should be 5 digits in the decimal part. Use math.pi to get the value of pi.

8. An escape sequence is a combination of characters in python that begins with a back slash
(\) character followed by letters or digits. Escape sequences represent non-printable and
special characters. They have special meaning. Execute the following instructions to
understand the use of some escape sequences.

Escape Sequence Usage explanation


\n print(“Hello \n World”) New line
\t print(“Hello \t World”) tab
\bbb print(“\143”) Print the character
(a number after \) with number as the
octal code
\xaa print("\xAF") Print the character
(a two digit hex number with hex code aa
after \x)
\’ print (“It\'s raining”) To print ‘on the output
screen
\” Print(“He said \”Hello\””) To print “ on the
output screen

9. Controlling print function. It is possible to control how the print function visually seperates
the data on the screen. By default, the print function places a single space in between the
items it prints. The print function uses a keyword argument named sep to specify the string
to use insert between items. The name sep stands for separator. The default value of sep is
the string ' ', a string containing a single space.

Execute the following code and look at the output, to learn separator in print function.
w, x, y, z = 10, 15, 20, 25
print(w, x, y, z)
print(w, x, y, z, sep=',')
print(w, x, y, z, sep='')
print(w, x, y, z, sep=':')
print(w, x, y, z, sep='-----')
10. Preformatted printing. Print function in python allows to write multiline strings. An
important feature of multiline strings is that it obeys indentation, spaces in the string, and
line breaks. In other words, the string printed on the output screen looks exactly like that in
the code. Multiline string specified using ‘’’ and ‘’’. See the following example. Type the code
in the interpreter and see the output.
>>> x='''
1-----2
| |
| |
| |
3-----4
A parallelogram has 4 corners
'''
>>> print(x)

Operators in Python

11. First, predict the output of the following python programs. Then execute each in the
shell and check whether your predictions were correct or not.
1. 3/2
2. 3//2
3. 2<3
4. 2<3<4
5. 4<5<2
6. x=y=z Assume x=2, y=3 and z=4.
7. x=y+=z
8. +x (Assume x=-3)
9. -x (Assume x=-3)
10. not x (Assume x= 1
11. 1%4+2%4+3%4+4%4
12. X and Y
a. Assume X=2 and Y=2
b. Assume X=2 and Y=0
c. Assume X= True and Y=True
d. Assume X= False and Y=True
e. Assume X= False and Y=False
13. 2**4*3
14. 2+(3-2)
15. 12/3/2
16. “python” > “java”
17. ‘5’+’10’
Python Built-in functions
12. Write python code to print all keywords in the python version installed in your computer.
13. Write python code to print the built-in functions in the python version installed in your
computer.
14. Write python instructions to do the following operations
1. Square root of a number
2. n power p
3. display absolute value of a number
4. convert a decimal number into binary
5. convert a decimal number into octal
6. Find the largest and smallest among a set of numbers
7. Find absolute value of a number
8. Print the factorial of a number
9. Print a power b.
10. Read a number, print the sine, cosine and tangent of the
number.
11. Round a float number to r decimal points.
15. First predict the types of the following values. Then use type() built-in function to find
the types and compare your answers
1. 3
2. ‘3’
3. “3”
4. 3.5
5. 3j
6. [3]
16. Read a number and numeric character. Display the sum.
17. Write a Python program display a random number.
|𝑥−𝑦|
18. Write a program that asks the user to enter two numbers, x and y, and computes 𝑥+𝑦
Simple expressions
19. Write a program that takes an amount in Indian Rupees and the conversion rate of Euro
as inputs. Find its equivalent in Euro and display it.
20. Write a program which takes two numbers as input and print the smallest and the
largest (use built-in functions) .
21. Given the marks of five subjects, calculate and print the average marks.
22. Write a python program to find sum of first n natural numbers.
23. Create a python program to calculate the area of a triangle if the side lengths are given.
24. Write a Python program which accepts the radius of a circle from the user and compute
the area.
25. Given principle amount, interest rate and time find the simple interest.
26. Write a Python program to swap the values of two variables.
27. Write a Python program to convert height (in feet and inches) to centimeters.
28. Write a Python program to convert seconds to day, hour, minutes and seconds.
29. Write a program to read a 7 digit number and a number d, which is <=7. Then extract
the last d digits of the 7 digit number.
30. An elephant decided to visit his friend. It turned out that the elephant's house is located
at point 0 and his friend's house is located at point x(x > 0) of the coordinate line. In one
step the elephant can move 1, 2, 3, 4 or 5 positions forward. Determine, what is the
minimum number of steps he need to make in order to get to his friend's house.
31. To make a paper airplane, one has to use a rectangular piece of paper. From a sheet of
standard size you can make s airplanes. A group of k people decided to
make n airplanes each. They are going to buy several packs of paper, each of them
containing p sheets, and then distribute the sheets between the people. Each person
should have enough sheets to make n airplanes. How many packs should they buy?
32. Ted has a pineapple. This pineapple can bark like a bulldog! At time t (in seconds) it
barks for the first time. Then every s seconds after it, it barks twice with 1 second
interval. Thus it barks at times t, t + s, t + s + 1, t + 2s, t + 2s + 1, etc.

Barney woke up in the morning and wants to eat the pineapple, but he can't eat it when
it's barking. Barney plans to eat it at time x (in seconds), so he asked you to tell him if
it's gonna bark at that time.

You might also like