Lab 3 Elementary Programming 2
Lab 3 Elementary Programming 2
Code Once
Introduction to Computers
Laboratory Manual
Experiment #3
Elementary Programming, II
Experiment #3: Elementary Programming, II
A number that has a decimal point is a float even if its fractional part is 0. For example, 1.0 is
a float, but 1 is an integer.
In the programming terminology, numbers such as 1.0 and 1 are called literals. A literal is a
constant value that appears directly in a program.
Note that the + and - operators can be both unary and binary. A unary operator has only one
operand; a binary operator has two. For example, the - operator in -5 is a unary operator to
negate the number 5, whereas the - operator in 4 - 5 is a binary operator for subtracting 5
from 4.
2
Experiment #3: Elementary Programming, II
The / operator performs a float division that results in a floating number if one of the two
numbers is a float.
>>> 4.0 / 2
2.0
>>> 2.0 / 4
0.5
The / operator performs an integer division if the two numbers are integers; the result is an
integer, and any fractional part is truncated.
>>> 5 / 2
2
>>> 2 / 4
0
>>> 2 ** 3
8
The % operator, known as remainder or modulo operator, yields the remainder after division.
>>> 20 % 13
7
26 / 5.0 26 / 5
28 % 5 25 % 5
3 ** 3 45 + 43 % 5 * (23 * 3 % 2)
3
Experiment #3: Elementary Programming, II
Operators contained within pairs of parentheses are evaluated first. Parentheses can be nested,
in which case the expression in the inner parentheses is evaluated first.
4 3 + 3(2 + 1)
− 9(1 + 2 ∗ 3) +
3(2 + 34) 2 + 3 ∗ 12
Solution:
print(4.0 / (3 * (2 + 34)) - 9 * (1 + 2 * 3)
+ (3 + 3.0 * (1+2)) / (2 + 3 * 12))
Very often the current value of a variable is used, modified, and then reassigned back to the
same variable. For example, the following statement increases the variable count by 1:
count = count + 1
Python allows you to combine assignment and addition operators using an augmented
assignment operator. For instance, the preceding statement can be written as:
count += 1
4
Experiment #3: Elementary Programming, II
x = 5
y = str(x)
print("x",type(x))
print("y",type(y))
We also can convert to int from string, if the content of the string is an integer number.
i = "10"
j = int(i)
print("i",type(i))
print("j",type(j))
5
Experiment #3: Elementary Programming, II
When converting from a float number to an integer number the fraction part is truncated.
x = 10.5
y = int(x)
print(y)
10
You can also use the round function to round a number to the nearest whole value.
x = 10.6
y = round(x)
print(y)
11.0
Lab Work
Ex1: Write a Python Program that split up a given number of days to years, months, weeks, and
days.
For Example: If the user enters 410 days, the program prints 1 year, 1 month, 2 weeks, 1 day
Solution:
Ex2: Write a Python program that reads two points (ex, p1(5,5) and p2(3.5, -1.5)), then computes
the distance between these two points. The formula for computing distance between two points
is as follows
6
Experiment #3: Elementary Programming, II
Solution:
Homework
1. Assume that a = 1, and that each expression is independent. What are the results of the
following expressions?
a += 4
a -= 4
a *= 4
a /= 4
a /= 4.0
a %= 4
a = 56 * a + 6
2. What does a conversion from a float to an integer do with the fractional part of the
float value? Does the int(value) function change the variable value?
3. Write a program that prompts the user to enter a four-digit integer and displays the
number in reverse order.
Enter an integer: 1234
1
2
3
4
4. Write a program that prompts the user to enter the side of a hexagon and displays its
3√3 2
area. The formula for computing the area of a hexagon is 𝐴 = 𝑠 where s is the
2
length of a side.
7
Experiment #3: Elementary Programming, II
Good Luck