0% found this document useful (0 votes)
304 views12 pages

1write A Python Program To Generate Electricity Bill

The document contains several Python code examples for tasks like generating electricity bills, checking if a number is positive or negative, swapping two numbers, finding the sum of odd and even numbers, calculating factorials using recursion, counting vowels in a string, calculating the area of different shapes, reversing a string word by word, simulating a bouncing ball using Pygame, and removing duplicates from a list.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
304 views12 pages

1write A Python Program To Generate Electricity Bill

The document contains several Python code examples for tasks like generating electricity bills, checking if a number is positive or negative, swapping two numbers, finding the sum of odd and even numbers, calculating factorials using recursion, counting vowels in a string, calculating the area of different shapes, reversing a string word by word, simulating a bouncing ball using Pygame, and removing duplicates from a list.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 12

1 Write a Python program to generate Electricity Bill

2 Python program to find the Biggest of 3 numbers using nested


if...elsestatement
3 a Python program to find whether the given number is Positive or
Negativeusing if statement.
4 Swap the TwoNumbers
5 find the sum of odd and even numbers
6 python program to get the 5 subjects marks and display the grade
7 python program to find the sum of first ‘n’ natural number.
8 Python program to check if the given string is palindrome or not.
9 Print the common characters in both the strings and the count of
them.
10 a Python program to Find the Duplicate Element from a List.
11 Python program to add an item in a tuple using looping.

L = [5, 4, 2, 5, 6, 1]
res = []

for i in range(len(L)):
res.append((L[i], i))

print("List of Tuples")
print(res)

12 a Python Program to Find the Factorial of a Given Number Using


Recursion.
# Factorial of a number using recursion

def recur_factorial(n):
if n == 1:
return n
else:
return n*recur_factorial(n-1)

num = 7

# check if the number is negative


if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
print("The factorial of", num, "is", recur_factorial(num))
OUTPUT

The factorial of 7 is 5040

13 Python program to Count the number of Vowels in a string


String = input('Enter the string :')
count = 0
#to check for less conditions
#keep string in lowercase
String = String.lower()
for i in String:
if i == 'a' or i == 'e' or i == 'i' or i == 'o' or i == 'u':
#if True
count+=1
#check if any vowel found
if count == 0:
print('No vowels found')
else:
print('Total vowels are :' + str(count))

OUTPUT:
Output:
Enter the string :RAJESHAA
Total vowels are :3

14 Python program to find the Area of Shape.


# define a function for calculating
# the area of a shapes
def calculate_area(name):\
# converting all characters
# into lower cases
name = name.lower()

# check for the conditions


if name == "rectangle":
l = int(input("Enter rectangle's length: "))
b = int(input("Enter rectangle's breadth: "))

# calculate area of rectangle


rect_area = l * b
print(f"The area of rectangle is
{rect_area}.")

elif name == "square":


s = int(input("Enter square's side length: "))

# calculate area of square


sqt_area = s * s
print(f"The area of square is
{sqt_area}.")
elif name == "triangle":
h = int(input("Enter triangle's height length: "))
b = int(input("Enter triangle's breadth length: "))

# calculate area of triangle


tri_area = 0.5 * b * h
print(f"The area of triangle is
{tri_area}.")

elif name == "circle":


r = int(input("Enter circle's radius length: "))
pi = 3.14

# calculate area of circle


circ_area = pi * r * r
print(f"The area of circle is
{circ_area}.")

elif name == 'parallelogram':


b = int(input("Enter parallelogram's base length: "))
h = int(input("Enter parallelogram's height length: "))
# calculate area of parallelogram
para_area = b * h
print(f"The area of parallelogram is
{para_area}.")

else:
print("Sorry! This shape is not available")

# driver code
if __name__ == "__main__" :

print("Calculate Shape Area")


shape_name = input("Enter the name of shape whose area you want to
find: ")

# function calling
calculate_area(shape_name)

OUTPUT
Calculate Shape Area
Enter the name of shape whose area you want to find: rectangle
Enter rectangle's length: 10
Enter rectangle's breadth: 15
The area of rectangle is 150

15 python class to reverse a string word by word.

# Python code
# To reverse words in a given string

# input string
string = "geeks quiz practice code"
# reversing words in a given string
s = string.split()[::-1]
l = []
for i in s:
# apending reversed words to l
l.append(i)
# printing reverse words
print(" ".join(l))
16 Simulate Bouncing Ball Using Pygame
import pygame
# initialize pygame
pygame.init()

# define width of screen


width = 1000
# define height of screen
height = 600
screen_res = (width, height)

pygame.display.set_caption("GFG Bouncing game")


screen = pygame.display.set_mode(screen_res)

# define colors
red = (255, 0, 0)
black = (0, 0, 0)

# define ball
ball_obj = pygame.draw.circle(
surface=screen, color=red, center=[100, 100], radius=40)
# define speed of ball
# speed = [X direction speed, Y direction speed]
speed = [1, 1]

# game loop
while True:
# event loop
for event in pygame.event.get():
# check if a user wants to exit the game or not
if event.type == pygame.QUIT:
exit()

# fill black color on screen


screen.fill(black)

# move the ball


# Let center of the ball is (100,100) and the speed is (1,1)
ball_obj = ball_obj.move(speed)
# Now center of the ball is (101,101)
# In this way our wall will move
# if ball goes out of screen then change direction of movement
if ball_obj.left <= 0 or ball_obj.right >= width:
speed[0] = -speed[0]
if ball_obj.top <= 0 or ball_obj.bottom >= height:
speed[1] = -speed[1]

# draw ball at new centers that are obtained after moving ball_obj
pygame.draw.circle(surface=screen, color=red,
center=ball_obj.center, radius=40)

# update screen
pygame.display.flip()

OUTPUT:

17 Python program to remove Duplicates from a list

def Remove(duplicate):
final_list = []
for num in duplicate:
if num not in final_list:
final_list.append(num)
return final_list

# Driver Code
duplicate = [2, 4, 10, 20, 5, 2, 20, 4]
print(Remove(duplicate))

You might also like