Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

Python Functions

print("hello world")
"" => treated as word

= used to assign values


== used to check values

print(9-3) => treated as numbers

print(5%4) => remainder

print(9//2) => integer division, 9/2=4.5=4 (rounded off)

print(9**2) => power off


a=2
b=3
print(a**b)
2^3

print(10*"hello\n") => used for number of times to be written(10) and new lines(n)
print("hello\t") => used for tap (more space)

a=int(input("enter a num:")) => used to take input from the user, int = integer
print(a)

b=int(input("enter a num:"))
print(b)

if a>b:
print("a is max")
else:
print("b is max")
The code above is used for if and else statements

a=int(input("Enter your age to check your eligibility:"))

if a>18:
print("You are eligible to vote")
else:
print("No")

Q-Check whether a year is a leap year or not.

a=int(input("Enter the year:"))


if a%4==0:
print("Leap year")
else:
print("No")

Q- Check whether a number is divisible by 5 or not.


a=int(input("Enter the number to check if it is divisible by 5")

if a%5==0:
print("Yes")
else:
print("No")
Q- Check whether a question is positive, zero or negative.
if a>0:
print("Positive")
elif a==0:
print("Zero")
else:
print("Negative")

elif => else if ( used with if and else if there are more than two conditions)

Q- Take length and breadth measurements from the user to check whether it is a
rectangle or a square.
a=int(input("Enter the length"))
b=int(input("Enter the breadth"))

if a>b:
print("Rectangle")
elif b>a:
print("Rectangle")
else
print("Square")

Q- Check whether a number is even or odd.


a=int(input("Enter the number"))

if a==0:
print("Zero")
elif a%2==0:
print("Even")
else:
print("Odd")

Q- To check whether a triangle is equilateral, isosceles, or scalene.

if (a>b and b>c) or (a<b and b<c) or (b>a and a>c) or (a>b and c>b):
print("Scalene")

elif (b==a and a==c and b==c):


print("Equilateral")
else:
print("Isosceles")

To avoid writing such a long code, we can use '!=' for not equals to.

if (a!=b) and (a!=c) and (b!=c):


print("Scalene")
elif (b==a and a==c and b==c):
print("Equilateral")
else:
print("Isosceles")

Data types:

Dictionary
thisCar={"name":"sajdasjdh",
"age":545}
print(thisCar)

print(type(thisCar))
Dictionary length=> This function prints the name and age along with the type of
data.
print(len(thisdict))

for i in "python":
print(i)

You might also like