Programming Fundamentals Lab 03
Programming Fundamentals Lab 03
Experiment No. 03
Lab 03 –Python Operators.
Lab Objectives:
1. Python Operators
1. Python Operators
Operators are used to perform operations on variables and values. Python divides the operators in
the following groups:
1. Arithmetic operators
2. Assignment operators
3. Comparison operators
4. Logical operators
5. Identity operators
6. Membership operators
7. Bitwise operators
Arithmetic operators are used with numeric values to perform common mathematical operations:
Prepared By: Asst. Prof. Syed Faisal Ali CS-112 | Introduction to Programming Fundamentals 1
October 24, 2018 Lab 03 – Python Operators .
a = 10
b = 22
Output:
Prepared By: Asst. Prof. Syed Faisal Ali CS-112 | Introduction to Programming Fundamentals 2
October 24, 2018 Lab 03 – Python Operators .
x=5
x += 3
print(x)
x=5
x -= 3
print(x)
x=5
x *= 3
print(x)
x=5
x /= 3
print(x)
x=5
x%=3
print(x)
x=5
x//=3
Prepared By: Asst. Prof. Syed Faisal Ali CS-112 | Introduction to Programming Fundamentals 3
October 24, 2018 Lab 03 – Python Operators .
x=5
x **= 3
print(x)
x=5
x &= 3
print(x)
x=5
x |= 3
print(x)
x=5
x ^= 3
print(x)
x=5
x >>= 3
print(x)
x=5
x <<= 3
print(x)
Output:
Prepared By: Asst. Prof. Syed Faisal Ali CS-112 | Introduction to Programming Fundamentals 4
October 24, 2018 Lab 03 – Python Operators .
== Equal x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
x = 20
y = 15
Output:
Prepared By: Asst. Prof. Syed Faisal Ali CS-112 | Introduction to Programming Fundamentals 5
October 24, 2018 Lab 03 – Python Operators .
x = 15
print(x > 13 and x < 20)
x = 25
print(x > 23 or x < 24)
x = 35
print(not(x > 33 and x < 40))
Output:
Prepared By: Asst. Prof. Syed Faisal Ali CS-112 | Introduction to Programming Fundamentals 6
October 24, 2018 Lab 03 – Python Operators .
x = ["ahmed", "bashir"]
y = ["ahmed", "bashir"]
z=x
print(x is z)
print(x is y)
print(x == y)
Output:
x = ["ahmed", "bashir"]
y = ["ahmed", "bashir"]
z=x
print(x is not z)
print(x is not y)
print(x <> y)
Output:
Prepared By: Asst. Prof. Syed Faisal Ali CS-112 | Introduction to Programming Fundamentals 7
October 24, 2018 Lab 03 – Python Operators .
Output:
print("parkash" not in x)
Output:
Prepared By: Asst. Prof. Syed Faisal Ali CS-112 | Introduction to Programming Fundamentals 8
October 24, 2018 Lab 03 – Python Operators .
Program 9: You are planning to throw a small bird at a distance d, with time t, and height h to
some structure. Write a code in which you will use the physical quantities such as initial velocity,
final velocity, angle in radians, gravity, height, sling shot etc.
import math
# User inputs
velocity = float(input('Give me a velocity to fire at (in m/s): '))
angle = float(input('Give me an angle to fire at: '))
distance = float(input('Give me how far away you are from the structure: '))
height = float(input('Give me the height of the structure (in meters): '))
slingshot = 5 #Height of slingshot in meters
gravity = 9.8 #Earth gravity
# Calculations
time = distance/(velocity * x)
vx = x
vy = y + (-9.8 * time)
finalVelocity = math.sqrt((vx ** 2) + (vy ** 2))
Output:
Prepared By: Asst. Prof. Syed Faisal Ali CS-112 | Introduction to Programming Fundamentals 9
October 24, 2018 Lab 03 – Python Operators .
Programming Exercise
Solving real world problems of physics. Today we will focus on how we can solve the real
world problems by using Python programming language. So for this, I am giving you the
formulas of physics and you have to write the code to find the answers.
Question 1. A ball at the end of a string is revolving uniformly in a horizontal circle of radius 2 meters at
constant angular speed 10 rad/s. Determine the magnitude of the linear velocity of a point located:
(a) 0.5 meters from the center
(b) 1 meter from the center
(c) 2 meters from the center
Known: Radius (r) = 0.5 meters, 1 meter, 3 meters, The angular speed = 10 radians/second
Wanted: The linear velocity
Formula: v = r ω
Question 2. The blades in a blender rotate at a rate of 5000 rpm. Determine the magnitude of the linear
velocity:
(a) a point located 5 cm from the center
(b) a point located 10 cm from the center
Known: Radius (r) = 5 cm and 10 cm
The angular speed (ω) = 5000 revolutions / 60 seconds = 83.3 revolutions / second = (83.3)(6.28 radian) /
second = 523.3 radians / second
Wanted: The magnitude of the linear velocity
Formula: v = r ω
Question 3. A point on the edge of a wheel 30 cm in radius, around a circle at constant speed 10
meters/second.
What is the magnitude of the angular velocity?
Known: Radius (r) = 30 cm = 0.3 meters, The linear velocity (v) = 10 meters/second
Wanted: the angular velocity
Formula: v = r ω
Prepared By: Asst. Prof. Syed Faisal Ali CS-112 | Introduction to Programming Fundamentals 10
October 24, 2018 Lab 03 – Python Operators .
Known:
Radius (r) = 0.25 meter, The linear speed of a point on the edge of tires (v) = 10 meters/second
Wanted: The angular speed
Formula: v = r ω
Question 5. The angular speed of wheel 20 cm in radians is 120 rpm. What is the distance if the car travels
in 10 seconds.
Known: Radius (r) = 20 cm = 0.2 meters
The angular speed = 120 rev / 60 seconds = 2 rev / second = (2)(6.28) radians / second = 12.56 radians /
second
Wanted: distance
Formula: v = r ω
Question 6: A car is running at a velocity of 50 miles per hour and the driver accelerates the car by 10
miles/hr2. How far the car travels from this point in the next 2 hours, if the acceleration is constant.
Formula: v = u + at
Question 7: A Stone is dropped freely from a height of 100 feet. With what velocity will it hit the ground?
(Neglect the air resistance and assume the acceleration due to gravity is 32ft/s 2).
Formula: v2 – u2 = 2as
Prepared By: Asst. Prof. Syed Faisal Ali CS-112 | Introduction to Programming Fundamentals 11