0% found this document useful (0 votes)
10 views21 pages

PF Lec2

Good

Uploaded by

unb2238
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views21 pages

PF Lec2

Good

Uploaded by

unb2238
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Training Lecture 2

How to write a program

1. Calculate 2x^2

Required value of X => X^2


How to write a program

2. Calculate area of a triangle

area=½*h*b
How to write a program

2. Calculate area of a triangle

1. h = 5 # h = float(input('Enter first side: '))


2. b = 6 # b = float(input('Enter third side: '))
3. area = h*b*0.5 # calculate the area, area=h*b/2
4. print('The area of the triangle is %0.2f' %area)
How to write a program

3. Calculate area of a circle


How to write a program

4. Calculate area of a rectangle


How to write a program

5. Calculate 2^3

Required ???????????
How to write a program

6. Limit number of decimal points

num = 458.541315
print('%.2f' % num)
How to write a program

7. Accept any three string from one input() call

str1, str2, str3 = input("Enter three


string").split() print(‘FirstName:', str1)
print(‘Middle Name:', str2)
print(‘LastName:', str3)
How to write a program

Sometimes you need to change the type of a variable . This


concept is called typecasting.

n = float(input())
n = int(input())

C=input()
C=int(c )

num1 = int(input("Enter first number "))


num2 = int(input("Enter second number "))
result = num1 * num2
print("Multiplication is", result)
.
Output style
# print with end whitespace
print('Good Morning!', end= ' ')

# print with “-” separated


print(‘24', ‘01’, ‘2023.', sep= ‘- ')
print(‘Training ' + ‘ Day 2')

a=5
b = 10

print('The value of a is {} and b is {}'.format(a,b))


Week 1: How to write a program

8. You have marks of four subjects. Calculate average.


Week 1: How to write a program

9. You have marks of four subjects. Calculate the percentage


marks.
Week 1: How to write a program

10. Calculate salary of an employee based on hours spend.

Salary= total hours spend*amount per hour

What if increment based on hours spend? Or based on hours fulfilled?


Week 1: How to write a program

11. Convert Celsius scale to Fahrenheit scale


Operators

x+y : addition

x-y : subtraction

x*y : multiplication

x/y : division

x//y : returns the quotient


Floored division

x**y : power

x%y : mode or remainder after


division
==, equal > greater Operators
Comparison >= at least
!=, not equal < less <= at least
Conditional Statements
• Python have if-elif-else only
• Don’t have switch statement. Only way is to
use dictionary we will learn later.

Generalized syntax:
if condition:
#dothis
else:
#dothis
Conditional Statements
Equals: a == b
Not Equals: a != b
Less than: a < b
Less than or equal to: a <= b
Greater than: a > b
Greater than or equal to: a >= b
Conditional Statements
a = 400
b = 500
if b > a:
print("b is greater than a")
else:
print("a is greater than b")
Conditional Statements
a = 400
b = 500
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
Day 2- Tasks
6. You have marks of four subjects. Calculate the percentage of
marks.
7. Take input of temperature in celsius scale. Convert Celsius scale
to Fahrenheit scale.You can take help from slides.
8. Calculate salary of an employee based on hours spend. You can
take help from slides.
9. Take input from user for number a. Check whether the number is
an even/odd number.
Hint: use % modulus operator to get the remainder. i.e. in
statement c=a%2, a%2 assigned the remainder value to c.
10. Take two numbers from user as input. check the greater
number is divisor of four or not?

You might also like