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

Lab 3 Elementary Programming 2

Uploaded by

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

Lab 3 Elementary Programming 2

Uploaded by

Sudesh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Think Twice

Code Once

The Islamic University of Gaza


Engineering Faculty
Department of Computer Engineering
Fall 2017
LNGG 1003
Khaleel I. Shaheen

Introduction to Computers

Laboratory Manual

Experiment #3

Elementary Programming, II
Experiment #3: Elementary Programming, II

Numeric Data Types and Operators


The information stored in a computer is generally referred to as data. There are two types of
numeric data: integers and real numbers. Integer types are for representing whole numbers, for
example 5, 105 or 8848484. Real types are for representing numbers with a fractional part,
for example 5.5, 3.14 or 8797.5487.

How do we tell Python whether a number is an integer or a float?

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.

The operators for numeric data types are shown below

Name Meaning Example Result


+ Addition 34 + 1 35
- Subtraction 34.0 - .01 33.9
* Multiplication 300 * 30 9000
/ Division 4/2 2
** Exponentiation 4 ** 0.5 2.0
% Remainder 20 % 3 2

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

To compute ab (a with an exponent of b) you can write a ** b in Python.

>>> 2 ** 3
8

The % operator, known as remainder or modulo operator, yields the remainder after division.

>>> 20 % 13
7

Ex: What are the results of the following expressions?

26 / 5.0 26 / 5
28 % 5 25 % 5
3 ** 3 45 + 43 % 5 * (23 * 3 % 2)

3
Experiment #3: Elementary Programming, II

Evaluating Expressions and Operator Precedence


Python expressions are evaluated in the same way as arithmetic expressions. When more than
one operator is used in an expression, the following operator precedence rule is used to
determine the order of evaluation:

1. Exponentiation (**) is applied first.


2. Multiplication (*), division (/), and remainder operators (%) are applied next.
3. Addition (+) and subtraction (-) operators are applied last.

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.

Ex: Write the following arithmetic expression in Python

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))

Augmented Assignment Operators


The operators +, -, *, /, %, and ** can be combined with the assignment operator (=) to form
augmented assignment operators.

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

All augmented assignment operators are shown in the table below.

Operator Name Example Equivalent


+= Addition assignment i += 8 i=i+8
-= Subtraction assignment i -= 8 i=i-8
*= Multiplication assignment i *= 8 i=i*8
/= Division assignment i /= 8 i=i/8
%= Remainder assignment i %= 8 i=i%8
**= Exponent assignment i **= 8 i = i ** 8

Type Conversions and Rounding


The datatypes we studied so far are integer, float and string. We can convert between those
data types using the suitable method. For example, for converting to string we use the method
str.

x = 5
y = str(x)
print("x",type(x))
print("y",type(y))

('x', <type 'int'>)

('y', <type 'str'>)

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))

('i', <type 'str'>)

('j', <type 'int'>)

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:

days = input("Enter the number of days: ")


years = days / 365
days = days % 365
months = days / 30
days = days % 30
weeks = days / 7
days = days % 7
print(str(years) + " years, " + str(months) + " months, "
+ str(weeks) + " weeks, " + str(days) + " days.")

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:

x1, y1 = input("Enter point1: ")


x2, y2 = input("Enter point2: ")

distance = ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5

print("The distance between the two points is " + str(distance))

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

Enter the side: 7.5


The area of the hexagon is 146.1375

Good Luck

You might also like