Practice Lab 1 - Introduction To Python

You might also like

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

Design and Simulation Lab (CHCE 4111)

B. Tech Chemical Engineering Spl. Refining and Petrochemicals, Semester VII


UPES, Dehradun
AY – 2021 - 22
Dr. Nilanjana Banerjee Department of Chemical Engineering

Practice Lab – 1 Introduction to python programming


Instructions: Each question can be programmed as a cell. Create one single .py file and submit it
on blackboard 1 week from the date of uploading. Remember each programmer has his/her own
style of programming which is like a signature, unique. So, it becomes very apparent when you
have copied someone else’s work. You will be penalized for each copied work.

Calculating and printing:


Write a function 'def area(a,b,h)' to compute the area of a trapezoid: formula is area = ((a+b)/2)*h
Output should look like:
““The area of a trapezoid with bases 12 and 10 and height 12 is 132””
************************************************************************************************
Taking an input from user, calculating and printing:
The Gaussian density function is commonly used in engineering statistics to define the
distribution of a certain measurable quantity. This Gauss distribution function is given below:

𝟏 𝟐
𝒀= 𝒆−𝟎.𝟓𝑿
√𝟐𝝅
a) Write a function ‘def gauss(x)’ to read a value of X and compute the corresponding value
of Y. For best results choose x between 0 and 1. Output should look like
““The standard normal density function evaluated at x.xx gives a value of xx.xxxx””
b) Modify the above program by taking “x” as an input from the user using the “input()”
syntax and the output again should look like as given above. Remember in python the
input statement takes in anything as a “string” which has to be converted into a float or
an integer. For best results choose x between 0 and 1.
Note: remember to use exponent, import math and then use math.exp() to get the exponent value.
Easiest way to do is solve the exponent term separately and assign it to a variable which can be
used for further calculations.

************************************************************************************************

Taking an input and printing a string:


Write a function ‘def name()’, which will take the name, city, state and school name as an input
from the use using the “input()” syntax and print it. Print where you are from on a new line. Put
the customary comma between city and state. The output should look like
“”My name is Nilanjana Banerjee
I stay in Dehradun, Uttarakhand
I go to University of Petroleum and Energy Studies””
Page 1 of 2
NB
Design and Simulation Lab (CHCE 4111)
B. Tech Chemical Engineering Spl. Refining and Petrochemicals, Semester VII
UPES, Dehradun
AY – 2021 - 22
Dr. Nilanjana Banerjee Department of Chemical Engineering
************************************************************************************************
Conditional statements:
Write a function ‘def velocity()’ where you take the time as an input from the use using the
“input()” syntax and check conditions using the conditional “if/else/elif statements” and calculate
the velocity and print it. The conditions are:
𝟏𝟎 𝟐
𝑽= 𝒕 , 𝒊𝒇 𝟎 ≤ 𝒕 < 𝟏𝟎
𝟔𝟎
(𝟏𝟎𝟎𝟎 − 𝟓𝒕)
𝑽= , 𝒊𝒇 𝟏𝟎 ≤ 𝒕 ≤ 𝟐𝟎
𝟓𝟎
𝟒𝟓 𝟐
𝑽= + (𝒕 − 𝟐𝟎)𝟐 , 𝒊𝒇 𝒕 > 𝟐𝟎
𝟔𝟎 𝟔𝟎
Remember in python the input statement takes in anything as a “string” which has to be converted
into a float or an integer.

The output should look like


“”At time t = xx.x the velocity is xx.xx””
************************************************************************************************
Loops
Write a function ‘def sum(n)’ that adds up the even numbers 1 through n and prints out the result.
You should use either a 'while' loop or a 'for' loop. Be sure that you check your answer on several
numbers n. Be careful that your loop steps through all the numbers from 1 through and including
n.
Check: if n=10, answer should be 30.
************************************************************************************************

Page 2 of 2
NB

You might also like