Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
25 views

Python Programs

The document discusses various Python programming concepts like variables, data types, operators, conditional statements, loops, functions, lists, tuples, sets and more with examples. It covers basic Python syntax and features through small code snippets and explanations.

Uploaded by

Shyam Mukhiya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Python Programs

The document discusses various Python programming concepts like variables, data types, operators, conditional statements, loops, functions, lists, tuples, sets and more with examples. It covers basic Python syntax and features through small code snippets and explanations.

Uploaded by

Shyam Mukhiya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

# first python program

print("hello","world")

2. type of variable

a=5
b="john"
print("my name is",b);
print(type(b)) # will give datatype of variable

3. Literals

a=0b101 #binary literals


b=10 #Decimal literals
c=0o123 #Octal literals
d=0x12c # hexadecimal literals
print(a,b,c,d)

4. expressions

"hello" + "world" #an expression


msg="hello"+"world" # statement
print(msg)

5. compound operator

#compound operator
x=5
x+=1
print(x)
#comparison operator
a=3
b=4
c=a<b
print(c)

6. Input from user

#receive input from keyboard


a=int(input("enter a number1 "))
b=int(input("enter a number2 "))
c=a+b
print("addition of two number is",c)
7. If Condition, If-else,elif,nested-if

#write the program on if condition


a=int(input("enter the value"))
b=int(input("entre the value"))
if(a<b):
print ("lesser")

#program on if-else condition


a=int(input("enter number"))
if (a>5):
print("number is greater than 5")
else:
print(a, "is not greater than 5")
#program on elif condition
a=int(input("enter number"))
if(a>6):
print(a,"is greater than 6")
elif(a<6):
print(a,"is not greater than 6")
else:
print(a,"is equal to 6")

#program on nested if condition


a=int(input("enter number"))
b=int(input("enter number"))
c=int(input("enter number"))
if(a>b):
if(a>c):
print(a,"is greater than b & c")
if(b>a):
if(b>c):
print(b,"is greater than a & c")
if(c>a):
if(c>b):
print(c,"is greater than a &b")

8. list

# list datatype
list=[]
list1=[1,2,3,4]
list2=["neha",1,2.0,5,6,7,8]
print(list2[1:5]) # slice operator
print(list2[-1]) # print last element
list1.append(0) # add element in list
print(list1)

#multi dimensional list


list=[['neha','is','pretty'],['i','am','ready']]
print(list)
list1=['hello']
list2=['list']
list1.append('a')
list1[0]=10 # change the value at index 0
# concatenate
print(list1+list2)
print(len(list1))

9. Tuple

#tuple datatype
tuple1=('a',1,2,"hello")
print(tuple1)
print(tuple1[1:])
print(tuple1[2])

10. Set

# set datatypes
set1={8,7,4,9,5}
set2={3,2,6,1}
print(set)
print("union of set is",set1.union(set2))

11. For-loop

# for loop, square of numbers


list=[1,2,3,4,5]

for val in list:


sq=val*val
print(sq)

for-loop using range()

# forloop using range, squares of first 5 natural numbers

for var in range(5):


#print(var)
sqr=var*var
print(sqr)

Table of 5

# print table of 5
for var in range(1,11):
mul=5*var
print("5*%d="%var,mul)

Number prime or not

#WAP for number is prime or not


n=int(input("enter a number"))
flag=False
for var in range(2,n):
if(n%var==0):
flag=True
if flag:
print("number is not prime")
else:
print("number is prime")

Factorial of a number

# WAP for factorial of number


n=int(input("enter number"))
fact=1
for i in range(1,n+1):
fact=fact*i
print("fact of", n, "is",fact)

sum of digits of a number using while loop

# WAP to sum of digits of a number


n=int(input("enter number"))
sum=0
while n>0:
digit=n%10
sum=sum+digit
n//=10
print(sum)

even or odd number

#WAP for even or odd


for i in range(1,11):
if(i%2==0):
print(i, "is even")
else:
print(i,"number is odd")
reverse of number & check palindrome number

#reverse of a number & check palindrome.


n=int(input("enter number"))
temp=n
rev=0
while n!=0:
dig=n%10
rev=rev*10+dig
n//=10
print(rev)
if(rev==temp):
print("number is palindrome")
else:
print("not palindrome")

continue & break statement

for i in "hello":
if(i=='e'):
#continue
break
print(i)
print(i)

swapping of number without using third variable

# WAP swapping of number without using third variable


a=int(input("enter first number"))
b=int(input("enter second number"))
a=a+b
b=a-b
a=a-b
print("after swapping value of a & b:",a,b)

swapping of numbers using third variable

# WAP swapping of number using third variable


a=int(input("enter first number"))
b=int(input("enter second number"))
c=a
a=b
b=c
print(a,b)

You might also like