0% found this document useful (0 votes)
3 views6 pages

Python Notes 2

The document provides a comprehensive overview of Python programming concepts, including variables, operators, data types, and control structures. It includes examples for evaluating expressions, swapping numbers, calculating averages, and implementing functions like recursion and lambda. Additionally, it covers practical applications such as calculating electricity bills and discounts, as well as programming exercises like FizzBuzz and finding prime numbers.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

Python Notes 2

The document provides a comprehensive overview of Python programming concepts, including variables, operators, data types, and control structures. It includes examples for evaluating expressions, swapping numbers, calculating averages, and implementing functions like recursion and lambda. Additionally, it covers practical applications such as calculating electricity bills and discounts, as well as programming exercises like FizzBuzz and finding prime numbers.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Python Programming – Question and Answers

1. Explain the concept of variables and identifiers in Python.

Variables store values. Identifiers are names for variables.

Improper naming causes errors and reduces readability.

Example: 2name = 5 → invalid, name_1 = 5 → valid.

2. Program to evaluate expressions:

print(2+4**2/4-(5-3))

print(2+4**2//4-(5-3))

Maximum of three numbers:

a,b,c = 10,20,15

print(max(a,b,c))

3. Comments and indentation:

# Single line comment

'''

Multi-line comment

'''

print("A"); print("B")

Indentation error occurs if block is not aligned properly.

4. Swap numbers:

With temp:

a,b = 5,10

temp=a

a=b

b=temp

Without temp:
a,b=b,a

Check datatype:

x=input()

print(type(x))

5. Average of marks and salary increment:

marks=[int(input()) for i in range(5)]

print(sum(marks)/5)

salary=35000

print(salary+salary*0.10)

6. IDLE vs command line vs script:

Script mode is best for professional development.

7. Operators classification:

Arithmetic(+,-), Relational(>,<), Logical(and,or), Assignment(+=),

Bitwise(&), Membership(in), Identity(is).

8. Sequence data types:

List, Tuple, String – support indexing and slicing.

9. Membership and identity:

emp1=[101,102,103]

emp2=emp1

new=int(input())

print(new in emp1)

print(emp1 is emp2)

10. Data types in online shopping:

int, float, str, bool, list, tuple, dict, set.

11. Comparison and logical operators:

a=5;b=3
print(a>b and a>0)

12. Mutable vs immutable:

List mutable, Tuple immutable.

13. Assignment and special operators:

x=5; x+=2

print(x)

print(3 in [1,2,3])

14. Bitwise and logical:

a=5;b=3

print(a&b, a|b)

15. Conditional statement example:

x=10

if x>5:

print("Greater")

16. Sum of odd and even:

n=int(input())

even=odd=0

for i in range(1,n+1):

if i%2==0: even+=i

else: odd+=i

print(even,odd)

17. Largest until done:

largest=None

while True:

data=input()

if data=="done": break
num=int(data)

if largest is None or num>largest:

largest=num

print(largest)

18. Prime numbers 1 to 50:

for num in range(2,51):

for i in range(2,num):

if num%i==0:

break

else:

print(num)

19. GCD and sum of digits:

import math

print([Link](12,18))

n=123

s=0

while n>0:

s+=n%10

n//=10

print(s)

20. Electricity bill:

units=int(input())

if units<=100: bill=units*2

elif units<=200: bill=100*2+(units-100)*3

else: bill=100*2+100*3+(units-200)*5

print(bill)
21. Lambda function:

square=lambda x:x*x

print(square(5))

22. Recursion factorial:

def fact(n):

if n==0:return 1

return n*fact(n-1)

23. Area function:

import math

def calculate_area(shape,dim):

if shape=="rectangle": return dim[0]*dim[1]

elif shape=="square": return dim[0]**2

elif shape=="circle": return [Link]*dim[0]**2

24. FizzBuzz:

for i in range(1,101):

if i%3==0 and i%5==0: print("FizzBuzz")

elif i%3==0: print("Fizz")

elif i%5==0: print("Buzz")

else: print(i)

25. Discount program:

amt=float(input())

if amt<1000: d=0

elif amt<=5000: d=amt*0.05

else: d=amt*0.10

print(d)
26. ATM logical operators:

if pin and balance>=amount and limit_ok:

print("Withdraw allowed")

You might also like