Ternary operations
I don’t assume you’re familiar with other programming languages,
but we need to mention this for those readers who are familiar with other languages.
Many languages have a shorthand way of doing if . . . else all on one line
of code.
If there are three condition we can use “if, elif,else” as
age = int(input(‘enter your age ’))
if age < 20:
print("don't be so childish")
elif 60>age>20:
print("Enjoy your days")
else:
print("Save your strength")
Output
enter your age 36
Enjoy your days
,Looping in python: -
Let us create a table showing the variation of height and range of a projectile with
which is projected at an angle 30 degree with vertical with initial speed 5 m/s.
To do that we have to solve (h=u*cos(a)*t-0.5*g*t**2) and (R=u**2*sin(2*a)/g), where
g=9.8 m/s2; a= 30degree angle; ‘h’ is height; and ‘R’ is range.
We can do the job by two ways.
1. Following the regular way i.e. put the value of t in the formulae and find the val
and ‘R’.
2. Or make a program to do the repetitive tedious job by looping the instruction. Wit
time, range.
While loops: -
Our tusk is to create a table containing time data and corresponding height data. Looping means doing a particular jo
again but with some different values of variables. To do the job, let us find, how while loop works.
1
Relation between height and time is given as ℎ = 𝑢 cos 𝜃. 𝑡 − 𝑔𝑡 2 . For some previous values of constant paramete
2
value of t, we will get a value of h. By repeating this process, we can produce a table. So to repeat the stapes we sho
loop.
, Algorithm: from math import *
a=cos(pi/6); b=sin(pi/6);
1. Print the table variables with proper
uv=5.0*a; uh=5.0*b; g= 9.81; t=0.0; dt=0.1; T=2.0*u
indentation.
while t <= T:
2. Assign the values of constants at t=0 h=-uv*t+0.5*g*t**2
s. R=2.0*uh*uv/g
3. Assign the minimum value of time print('at time %0.2f s, Hight of object %3.2f in m an
(independent variable). %2.2f m'%(t,h,R))
4. Assign the step of increment. t=t+dt
5. Write down the condition or upper
output
bound for reptation using while at time 0.00 s, Hight of object 0.00 in m and Range
statement. at time 0.10 s, Hight of object -0.38 in m and Range
6. Formula or relation between variables. at time 0.20 s, Hight of object -0.67 in m and Range
7. Print the values of variables at time 0.30 s, Hight of object -0.86 in m and Range
8. Increase the steps at time 0.40 s, Hight of object -0.95 in m and Range
9. Declare the end of the program. at time 0.50 s, Hight of object -0.94 in m and Range
at time 0.60 s, Hight of object -0.83 in m and Range
at time 0.70 s, Hight of object -0.63 in m and Range
at time 0.80 s, Hight of object -0.32 in m and Range
I don’t assume you’re familiar with other programming languages,
but we need to mention this for those readers who are familiar with other languages.
Many languages have a shorthand way of doing if . . . else all on one line
of code.
If there are three condition we can use “if, elif,else” as
age = int(input(‘enter your age ’))
if age < 20:
print("don't be so childish")
elif 60>age>20:
print("Enjoy your days")
else:
print("Save your strength")
Output
enter your age 36
Enjoy your days
,Looping in python: -
Let us create a table showing the variation of height and range of a projectile with
which is projected at an angle 30 degree with vertical with initial speed 5 m/s.
To do that we have to solve (h=u*cos(a)*t-0.5*g*t**2) and (R=u**2*sin(2*a)/g), where
g=9.8 m/s2; a= 30degree angle; ‘h’ is height; and ‘R’ is range.
We can do the job by two ways.
1. Following the regular way i.e. put the value of t in the formulae and find the val
and ‘R’.
2. Or make a program to do the repetitive tedious job by looping the instruction. Wit
time, range.
While loops: -
Our tusk is to create a table containing time data and corresponding height data. Looping means doing a particular jo
again but with some different values of variables. To do the job, let us find, how while loop works.
1
Relation between height and time is given as ℎ = 𝑢 cos 𝜃. 𝑡 − 𝑔𝑡 2 . For some previous values of constant paramete
2
value of t, we will get a value of h. By repeating this process, we can produce a table. So to repeat the stapes we sho
loop.
, Algorithm: from math import *
a=cos(pi/6); b=sin(pi/6);
1. Print the table variables with proper
uv=5.0*a; uh=5.0*b; g= 9.81; t=0.0; dt=0.1; T=2.0*u
indentation.
while t <= T:
2. Assign the values of constants at t=0 h=-uv*t+0.5*g*t**2
s. R=2.0*uh*uv/g
3. Assign the minimum value of time print('at time %0.2f s, Hight of object %3.2f in m an
(independent variable). %2.2f m'%(t,h,R))
4. Assign the step of increment. t=t+dt
5. Write down the condition or upper
output
bound for reptation using while at time 0.00 s, Hight of object 0.00 in m and Range
statement. at time 0.10 s, Hight of object -0.38 in m and Range
6. Formula or relation between variables. at time 0.20 s, Hight of object -0.67 in m and Range
7. Print the values of variables at time 0.30 s, Hight of object -0.86 in m and Range
8. Increase the steps at time 0.40 s, Hight of object -0.95 in m and Range
9. Declare the end of the program. at time 0.50 s, Hight of object -0.94 in m and Range
at time 0.60 s, Hight of object -0.83 in m and Range
at time 0.70 s, Hight of object -0.63 in m and Range
at time 0.80 s, Hight of object -0.32 in m and Range