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

python programs

The document contains various Python code snippets demonstrating different programming concepts such as generating a pyramid pattern, calculating the area of a triangle, swapping variables, generating random numbers, converting Celsius to Fahrenheit, displaying a calendar, and checking for positive/negative numbers. It also includes implementations for checking odd/even numbers, leap years, prime numbers, factorials, Fibonacci series, Armstrong numbers, and reversing an array. Each code example is self-contained and illustrates a specific functionality or algorithm.
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)
1 views

python programs

The document contains various Python code snippets demonstrating different programming concepts such as generating a pyramid pattern, calculating the area of a triangle, swapping variables, generating random numbers, converting Celsius to Fahrenheit, displaying a calendar, and checking for positive/negative numbers. It also includes implementations for checking odd/even numbers, leap years, prime numbers, factorials, Fibonacci series, Armstrong numbers, and reversing an array. Each code example is self-contained and illustrates a specific functionality or algorithm.
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
You are on page 1/ 7

# PYRAMID

# def py(n):

# for i in range(n):

# for j in range(n-i-1):

# print(" ",end="")

# for j in range(i+1):

# print("*",end=" ")

# print()

# s=py(6)

# AREA OF TRIANGLE

# num1=float(input("enter base:"))

# num2=float(input("enter ht:"))

# area=0.5*num1*num2

# print(area)

# print(f"area is: {area}")

#SWAP TWO VARIABLES

# v1=input("enter value of var1:")

# v2=input("enter value of var2:")

# print("original values","\nvar1:",v1,"\nvar2:",v2)

# temp=v1

# v1=v2

# v2=temp

# print("after swapping","\nvar1:",v1,"\nvar2:",v2)

#SWAP TWO VARIABLES WITHOUT TEMP VARIABLE

# v1=input("enter value of var1:")

# v2=input("enter value of var2:")

# v1,v2=v2,v1

# print("After swapping:")

# print("v1=",v1)

# print("v2=",v2)
#another way:

# v1=int(input("enter value of var1:"))

# v2=int(input("enter value of var2:"))

# v1=v1+v2

# v2=v1-v2

# v1=v1-v2

# print("After swapping:")

# print("v1=",v1)

# print("v2=",v2)

# GENERATE A RANDOM NUMBER

# import random

# num=random.random()

# print(num)

# or

# import random

# list=[21,13,2,4,0]

# print(random.choice(list))

# str="hello i'm vyshu from hyderabad"

# print(random.choice(str))

# CONVERT CELSIUS TO FAHRENHEIT

# cel=float(input("enter temperature in celsius:"))

# fahrenheit=cel*9/5+32

# print(cel,"degrees celsius is equal to",fahrenheit,"degrees fahrenheit")

# DISPLAY CALENDAR

# import calendar

# year=int(input("enter year:"))

# month=int(input("enter month:"))

# cal=calendar.month(year,month)

# print(cal)
# CHECK IF THE NUMBER IS POSITIVE, NEGATIVE OR ZERO

# num=float(input("enter a number:"))

# if(num>0):

# print("Given number is positive...")

# elif(num==0):

# print("Given number is zero...")

# else:

# print("Given number is negative...")

# CHECK GIVEN NUMBER IS ODD OR EVEN

# num=int(input("enter a number:"))

# if(num%2==0):

# print("Given number is even...")

# else:

# print("Given number is odd...")

#check leap year

# year=int(input("enter year:"))

# if((year%400==0) or (year%4==0 and year%100!=0)):

# print("leap year")

# else:

# print("not a leap year")

# CHECK GIVEN NUMBER IS PRIME OR NOT

# n=int(input("enter a number:"))

# if(n>1):

# for i in range(2,n):

# if(n%i==0):

# print("Not a prime number..")

# break

# else:

# print("prime number..")
# PRINT PRIME NUMBER FROM 1 TO 100

# n=int(input("enter range number:"))

# for i in range(1,n):

# count=0

# for j in range(1,i+1):

# if(i%j==0):

# count+=1

# if(count==2):

# print(i)

#FACTORIAL OF A NUMBER

#using for loop:

# n=int(input("enter a number:"))

# sum=1

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

# sum*=i

# print(sum)

#using while loop:

# fact=1

# i=1

# while (i<=5): # here we initialize number(5) directly

# fact*=i

# i+=1

# print(fact)

#DISPLAY MULTIPLICATION TABLE

# n=int(input("enter a number:"))

# for i in range(1,11):

# print(n,"*",i,"=",n*i)
# SUM OF NATURAL NUMBERS

#using for loop:

# n=int(input("enter a number:"))

# sum=0

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

# sum+=i

# print(sum)

#using while loop:

# n=int(input("enter a number:"))

# sum=0

# i=1

# while (i<=n):

# sum+=i

# i+=1

# print(sum)

# FIBONACCI SERIES

# def fib(n):

# a,b=0,1

# print(a)

# print(b)

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

# c=a+b

# a=b

# b=c

# print(c)

# fib(15)
# ARMSTRONG NUMBER

# n=int(input("enter a number:"))

# length=len(str(n))

# temp=n

# sum=0

# while temp>0:

# digit=temp%10

# sum+=digit**length

# temp=temp//10

# if(sum==n):

# print("Number is Armstrong no.")

# else:

# print("Not an armstrong no.")

# ARMSTRONG NUMBER IN GIVEN INTERVAL

# n=int(input("enter range:"))

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

# length=len(str(i))

# sum=0

# for j in str(i):

# sum+=int(j)**length

# if(sum==i):

# print(i)

#array in reverse order


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

array.reverse()

print(array)
Code implementation using Slicing method

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

reversed_array = array[::-1]

print(reversed_array)

using loop:-

1. #Initialize array

2. arr = [1, 2, 3, 4, 5];

3. print("Original array: ");

4. for i in range(0, len(arr)):

5. print(arr[i]),

6. print("Array in reverse order: ");

7. #Loop through the array in reverse order

8. for i in range(len(arr)-1, -1, -1):

9. print(arr[i]),

Output:

Original array:

12345

Array in reverse order:

54321

Palindrome
l=["e","i","a","e"]
copylist=l.copy()
copylist.reverse()
print(l)
print(copylist)
if(copylist==l):
print("palindrome")
else:
print("not")

You might also like