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

Basic python programs.py

The document contains a series of Python code snippets demonstrating various programming concepts such as arithmetic operations, variable manipulation, number properties, and string handling. Each snippet addresses a specific problem or task, including addition, swapping variables, checking for prime numbers, and finding factorials. The examples serve as practical exercises for learning basic programming techniques in Python.

Uploaded by

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

Basic python programs.py

The document contains a series of Python code snippets demonstrating various programming concepts such as arithmetic operations, variable manipulation, number properties, and string handling. Each snippet addresses a specific problem or task, including addition, swapping variables, checking for prime numbers, and finding factorials. The examples serve as practical exercises for learning basic programming techniques in Python.

Uploaded by

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

#_1 how to add two number

'''a=int(input())
b=int(input())
c=a+b
print(c)'''

#_2 How to swap two variable by using 3rd variable


'''a=int(input())
b=int(input())
c=a
a=b
b=c
print(a,b)'''

#_3 how to swap two variable without using 3rd variable


'''a=int(input())
b=int(input())
a=a+b
b=a-b
a=a-b
print(a,b)'''

#_4 how to swap 3 variable without using any variable


'''a=int(input())
b=int(input())
c=int(input())
a=a+b+c
b=a-b-c
c=a-b-c
a=a-b-c
print(a,b,c)'''

#_5 sum of digits


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

#_6 reversing a number


'''n=int(input("enter your number"))
sum=0
while n>0:
b=n%10
sum=sum*10+b
n=n//10
print(sum)'''

#_7 palindromme number (A number which is same from left and


right)
'''n=int(input("enter your number"))
a=n
sum=0
while n>0:
b=n%10
sum=sum*10+b
n=n//10
if (sum==a):
print("palindrome number")
else:
print("not a palindrome number")'''

#_8 even odd number


'''n=int(input("enter your number"))
if (n%2==0):
print("even number")
else:
print("odd number")'''

#_9 leap year


'''n=int(input("enter your number"))
if (n%400==0):
print("leap year")
elif (n%4==0 and n%100!=0):
print("leap year")
else:
print("not a leap year")'''

#_10 product of digits


'''n=int(input("enter your number"))
product=1
while n>0:
b=n%10
product=product*b
n=n//10
print(product)'''

#11_spy number (sum of digits ==product of digits)


'''n=int(input("enter your number"))
sum=0
product=1
while n>0:
b=n%10
sum=sum+b
product=product*b
n=n//10
if (sum==product):
print("spy number")
else:
print("not a spy number")'''

#_12 perfect number (sum of its factor excluding the no itself


is equal to that number)
'''n=int(input("enter your number"))
sum=0
for i in range (1,n//2+1):
if n%i==0:
sum=sum+i
if (sum==n):
print("perfect number")
else:
print("not a perfect number")'''

#_13 factorial
'''n=int(input("enter your number"))
factorial=1
while n>0:
factorial=factorial*n
n=n-1
print(factorial)'''

#_14 prime number


'''n=int(input("enter your number"))
if (n<=1):
print("not prime")
else:
flag=0
for i in range (2,n//2+1):
if (n%i!=0):
print("prime number")
flag=1
break
if (flag==0):
print("not a prime number")'''
#_15 fibbonaci series(0,1,1,2,3,5,8,13,21........)
'''n=int(input("enter your number"))
if n==1:
print(0)
if n==2:
print(0,1)
else:
print(0)
print(1)
n=n-2
a=0
b=1
while n>0:
c=a+b
a=b
b=c
print(c)
n=n-1'''
#_16 armstrong number
'''(It is a no in which we do sum of each digit raised to the
power of
total digits in the no if the sum becomes equals to the
original no
then we say that the no is armstrong no)'''

'''n=int(input("enter your number"))


a=n
count=0
while n>0:
count=count+1
n=n//10
n=a
sum=0
while n>0:
b=n%10
sum=sum+(b**count)
n=n//10
if (sum==a):
print("armstrong no")
else:
print("not a armstrong number")'''
#_17 strong number (sum of teh factorial of its digits
#is equal to the number itself)
'''from math import factorial
n=int(input("enter your number"))
sum=0
a=n
while n>0:
b=n%10
x=factorial(b)
sum=sum+(x)
n=n//10
if sum==a:
print("strong number")
else:
("not a strong number")'''

#_18 take input,reverse it then add to the original number


'''n=int(input("enter your number"))
sum=0
while n>0:
b=n%10
sum=sum*10+b
n=n//10
print(a+sum)'''

#_19 LCM of two number


'''a=int(input())
b=int(input())
if a==b:
print("lcm is",a)
else:
if (a>b):
max=a
else:
max=b
i=max
while (1): #i.e. 1!=0
if max%a==0 and max%b==0:
print("lcm is",max)
break
max=max+i'''

#_20 factorial using function(funcn is a set of


#instructions which is used to do a particular task)
'''def fact(n):
fact=1
while n>0:
fact=fact*n
n=n-1
return fact
n=int(input())
print(fact(n))'''

#_21 smallest number in 3 numbers


'''a=int(input())
b=int(input())
c=int(input())
x=c if c<a and c<b else b if b<a and b<c else a
print(x)'''

#_22 greatest number in 3 numbers


'''a=int(input())
b=int(input())
c=int(input())
x= c if c>a and c>b else b if b>c and b>a else a
print(x)'''

#_23 eval function ()


'''x=eval(input("enter your string/no"))'''

#_24 one line programme for sum of two digits


'''print("sum is",(int(input("enter your first number")))+
(int(input("enter your second number"))))'''

#_25 replacement operator


'''name = "gaurav"
city="bhopal"
weight=75.4
age=78
print(your name is {} your city is {} age is{} weight is
{}.format(name,city,age weight))'''

#_26 for writing indexing of whole string


'''s=input("enter a string")
x=0
for i in s:
print(i,"occured at ",x,"index")
x=x+1'''

#_27 linear search in strings


'''s=input("enter your string")
x=input("enter a character to search")
n=0
for i in s:
if i==x:
print("found")
n=1
break
if (n==0):
print("not found")'''

#_28 pattern printing


n=int(input("enter no of row"))
for i in range (1,n+1):
for j in range (1,i+1):
print("*",end="")
print()
#_29 to find the position of substring
'''s=input("enter your string")
sub=input("enter your substring")
flag=0
x=-1
while true:
x=s.find(sub,x+1,len(s))
if (x==-1):
break
print("found at",x,"index")
flag=1
if flag==0:
print("not found")'''

You might also like