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

82-Till End Getting Started With Python and Python Fundamentals Online

Hsh

Uploaded by

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

82-Till End Getting Started With Python and Python Fundamentals Online

Hsh

Uploaded by

Kapil Upadhyay
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 74

Taking input from user

Taking input from user


Taking input from user
Taking input from user
Taking input from user

What is wrong with the following statement(s)?


number=input(“number”)
Sqr=number*number
Taking input from user

What is wrong with the following statement(s)?


number=input(“number”)
Sqr=number*number

Sol:
number is of type string…hence can’t perform calculation
TASK: Taking input from user

Write code to obtain fee amount and then calculate fee hike
as 10% of fees and new amount(fee) to be paid.
TASK: Taking input from user

Write code to obtain fee amount and then calculate fee hike
as 10% of fees and new amount(fee) to be paid.
TASK: Taking input from user
TASK: Taking input from user
TASK: Taking input from user
TASK: Taking input from user
TASK: Taking input from user
TASK: Taking input from user
TASK:
TASK:
TASK:
TASK:
TASK:
TASK:
TASK:
TASK:
TASK:
TASK:
TASK:
TASK:
TASK:

What kind of program elements are the following


? ‘a’, 4.38925, “a”, “main” ?
TASK:

What kind of program elements are the following ? ‘a’,


4.38925, “a”, “main” ?

‘a’ single quotes string,


4.38925 fractional floating point literal,
“a” double quotes string,
“main” double quotes string
TASK:

What is the difference between full-line comment


and inline comment ?

Full line comments take a complete line while Inline comments


share the line with a statement or expression.
Full line:
# this is a full line comment
Inline:
a = 5 # this is an inline comment
TASK:
1. Write a program that displays a joke, but displays the
punchline only when the user presses the enter key. [HINT:
you may use input()]
1. Write a program that displays a joke, but displays the
punchline only when the user presses the enter key. [HINT:
you may use input()]

print('How many potatoes does it take to kill an irish farmer')


input('Press Enter’)
print('None')
2. Write a program that generates the following output :
5
10
9
Assign value 5 to a variable using assignment operator (=) Multiply it with 2 to generate 10 and
subtract 1 to generate 9.
2. Write a program that generates the following output :
5
10
9
Assign value 5 to a variable using assignment operator (=) Multiply it with 2 to generate 10 and
subtract 1 to generate 9.

# code a=5
a=5 print(a)
a=a*2
print(a) print(a)
a=a-1
print(a*2) print(a)

print(a*2-1)
3. Modify previous program so as to print output as 5@10@9.
3. Modify previous program so as to print output as 5@10@9.

# code
a=5
print(a, end='@’)
print(2*a, end='@’)
print(2*a-1)
4. Write the program with maximum three lines of code and that
assigns first 5 multiples of a number to 5 variables and then
print them.

x = eval(input("enter the number:"))


a,b,c,d,e = x*1,x*2,x*3,x*4,x*5
print(a , b , c , d , e )
4. Write the program with maximum three lines of code and that
assigns first 5 multiples of a number to 5 variables and then
print them.

# Code
a = int(input())
b, c, d, e = 2*a, 3*a, 4*a, 5*a
print(a, b, c, d, e)
5. Write a program that takes radius of a circle and prints area of
the circle
5. Write a program that takes radius of a circle and prints area of
the circle

#code
import math
R=eval(input(“enter radius”))
Area=math.pi*R*R
print(Area)
6. Write Python program that accepts marks in 5 subjects
and outputs average marks.
Python's map() is a built-in function that allows you to
process and transform all the items in an iterable without
using an explicit for loop, a technique commonly known
as mapping. map() is useful when you need to apply a
6. Write Python program that transformation function to each item in an iterable and
transform them into a new iterable.
accepts marks in 5 subjects and
outputs average marks. The split() method breaks up a string at
the specified separator and returns a list of
strings. Here, the separator is space ‘ ‘

Note the input (with space)


7. Write a short program that asks for your height in
centimeters and then converts your height to feet and
inches. (1 foot = 12 inches, 1 inch = 2.54 cm).

Expected output:
======================== RESTART: C:\python37\b11.py
========================
Enter your height in centimeters :150
Height is 4.0 feet and 11.055118110236222 inches
>>> 5/4 # / returns quotient in decimals
1.25
>>> 5//4 # //returns the integer part of the quotient if the numerator and the denominator are integers
1
>>> 5//4.0 # //returns the integer part of the quotient with .0 if either/both of the numerator and the
1.0 denominator is/are real number(s)
>>> 5.0//4
1.0
>>> 9%2 # % returns remainder
1
7. Write a short program that asks for your height in
centimetres and then converts your height to feet and
inches. (1 foot = 12 inches, 1 inch = 2.54 cm).

# Code
h = int(input('Enter your height in centimeters : ‘))
h = h/2.54 # height in inches
print('Height is', h//12, 'feet and', h%12, 'inches')
8. Write a program to read a number n and print n^2, n^3
and n^4.
8. Write a program to read a number n and print n^2, n^3
and n^4.

# Code
n = int(input())
print(n, n*n, pow(n, 3), pow(n, 4))
9. WAP to find area of an equilateral triangle[(√3/4)*a^2
9. WAP to find area of an equilateral triangle[(√3/4)*a^2

#code
import math
Side=eval(input(“Enter side”))
Area=math.sqrt(3)/4*pow(Side,2)
print(Area)
10. WAP to find area of a triangle[using Heron’s formula]
10. WAP to find area of a triangle[using Heron’s formula]

import math
a=eval(input("enter side"))
b=eval(input("enter side"))
c=eval(input("enter side"))
s=(a+b+c)/2
area=math.sqrt(s*(s-a)*(s-b)*(s-c))
print(area)
11. Write a program to compute simple interest and compound
interest.
11. Write a program to compute simple interest and compound
interest.
# Code
p = int(input('Pricipal Amount : ‘))
t = int(input('Time : ‘))
r = int(input('Rate(annum) : ‘))
Si = (p*r*t)/100
print('Simple Interest is ', Si)
Ci = p*pow((1+r/100), t) - p
print('Compound Interest is ', Ci)
12. Write a program to input a number and print its first five
multiples.
12. Write a program to input a number and print its first five
multiples.

# Code
a = int(input())
print(a, 2*a, 3*a, 4*a, 5*a)
13. Write a program to read details like name, class, age of a student and then
print the details firstly in same line and then in separate lines. Make sure to
have two blank lines in these two different types of prints.
13. Write a program to read details like name, class, age of a student and then
print the details firstly in same line and then in separate lines. Make sure to
have two blank lines in these two different types of prints.
# Code
name = input('Enter name : ‘)
class_ = input('Enter class : ') # class is a keyword, so use class_
age = input('Enter age : ‘)
print(name, class_, age)
print('\n’)
print(name)
print(class_)
print(age)
14. WAP to input a single digit(n) and print a 3 digit number created as
<n(n+1)(n+2)> e.g., if you input 7, then it should print 789. Assume that the
input is in range 1-7
14. WAP to input a single digit(n) and print a 3 digit number created as
<n(n+1)(n+2)> e.g., if you input 7, then it should print 789. Assume that the
input is in range 1-7
h=int(input("enter digit between 1-7"))
t=h+1
u=t+1
num=h*100+t*10+u*1
print (num)
15. Write a program to read three numbers in three variables and swap first
two variables with the sums of first and second, second and third numbers
respectively.
15. Write a program to read three numbers in three variables and swap first
two variables with the sums of first and second, second and third numbers
respectively.

# Code
a, b, c = map(int, input('Enter three numbers : ').split())
a, b = a+b, b+c
print(a, b, c)
16. Write a program that accepts cost of goods sold (cogs) revenue generated(rg),
operating costs (oc) and prints Gross profit(gp), net profit(np) and net profit
percentage(npp). [Hint : gross profit= Revenue generated–cost of goods sold
Net profit = gross profit – operating cost]
16. Write a program that accepts cost of goods sold (cogs) revenue generated(rg),
operating costs (oc) and prints Gross profit(gp), net profit(np) and net profit
percentage(npp). [Hint : gross profit= Revenue generated–cost of goods sold
Net profit = gross profit – operating cost]

# Code
cgos = int(input('Enter cost of goods sold : ‘))
rg = int(input('Enter revenue generated : ‘))
oc = int(input('Enter operating cost : ‘))
gp = rg - cogs # Gross profit
np = gp - oc # Net profit
npp = np/rg*100 # Net profit percentage
print('Gross profit is', gp)
print('Net profit is', np)
print('Net profit percentage is', npp)
17. Write a program to calculate in how many days a work will be completed by three persons A, B and C together.
A, B, C take x days, y days and z days respectively to do the job alone. The formula to calculate the number of days
if they work together is xyz/(xy + yz + xz) days where x, y, and z are given as input to the program.
17. Write a program to calculate in how many days a work will be completed by three persons A, B and C together.
A, B, C take x days, y days and z days respectively to do the job alone. The formula to calculate the number of days
if they work together is xyz/(xy + yz + xz) days where x, y, and z are given as input to the program.
18. Write a program to swap two numbers using a third variable.
18. Write a program to swap two numbers using a third variable.
18. Write a program to swap two numbers without using a third variable.
18. Write a program to swap two numbers without using a third variable.
Example of type casting
Example of type casting (explicit type conversion)
Example of type casting (explicit type conversion)
Example of implicit type conversion
☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺

You might also like