0% found this document useful (0 votes)
4 views13 pages

Python Code Examples for Beginners

The document contains a series of Python code snippets that perform various tasks such as comparing numbers, calculating averages, determining the area and perimeter of rectangles, checking for leap years, sorting student names, and performing statistical calculations on age data. Additionally, it includes code for reading CSV files, calculating HCF and LCM, and visualizing data using matplotlib. Each code snippet is designed to demonstrate basic programming concepts and functionalities in Python.

Uploaded by

mananbansal4in
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)
4 views13 pages

Python Code Examples for Beginners

The document contains a series of Python code snippets that perform various tasks such as comparing numbers, calculating averages, determining the area and perimeter of rectangles, checking for leap years, sorting student names, and performing statistical calculations on age data. Additionally, it includes code for reading CSV files, calculating HCF and LCM, and visualizing data using matplotlib. Each code snippet is designed to demonstrate basic programming concepts and functionalities in Python.

Uploaded by

mananbansal4in
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

Code 1 :

a=int(input("enter the no. "))

b=int(input("enter the no. "))

if (a>b):

print(a,"is greater than ",b)

elif(b>a):

print(b,"is greater than ",a )

else:

print("a is equal to b")

Code 2 :

a=int(input("enter the no. "))

b=int(input("enter the no. "))

c=int(input("enter the no. "))

average = (a+b+c)/2

print("the average of the data is ",average)

Code 3:

a=int(input("enter the lenght of rectangle"))

b=int(input("enter the breath of rectangle"))


m=str(input("enter the unit "))

perimeter = 2*(a+b)

area= a*b

print("the perimeter :",perimeter,"m")

print("the area :",area,"m" )

Code 4:

a=1

d=1

n=10

print("the natural no. are :")

for i in range (1,11):

print(i)

s=(n*(2*a+(n-1)*d))/2

print("the sum of first natural no. is :",s)

code 5

a = str(input("Enter the name of the subject: "))


Am = float(input(f"Enter the marks of the {a} out of 100: "))

b = str(input("Enter the name of the subject: "))

Bm = float(input(f"Enter the marks of the {b} out of 100: "))

c = str(input("Enter the name of the subject: "))

Cm = float(input(f"Enter the marks of the {c} out of 100: "))

avg = ((Am + Bm + Cm)/3)

print(f"The average of the subjects {a}, {b}, {c} is {avg}")

Code 6:

a=int(input("enter the year"))

if(a%4==0):

print(a,"is a leap year ")

else:

print(a,"is not a leap year")

Code 7:

a=int(input("enter the age "))

if(a>18):
print("u are eligeble to vote ")

elif(a==18):

print("this is your first year to vote")

else:

print("u are not eligeble to vote")

Code 8:

a = []

n = int(input("Enter the number of students: "))

for i in range(n):

students = input(f"Enter students {i+1}: ")

[Link](students)

print("List:", a)

[Link]()

print("the sorted list :",a)


Code 9:

element = []

n = int(input("enter the no. of element in first list: "))

for i in range(n):

a = input("enter the element in first list : ")

[Link](a)

print("first List:", element)

second_list=[]

N = int(input("enter the no. of element in second list: "))

for j in range(N):

N = input("enter the element in second list : ")

[Link](N)

print("the new list is :",element)


Code 10 :

import statistics

age = []

a = int(input("enter the no. of age "))

for i in range(a):

n = int(input("enter the age : "))

[Link](n)

print("the age is :", age)

mean = [Link](age)

median = [Link](age)

mode = [Link](age)

print("mean: ", mean)

print("median: ", median)

print("mode: ", mode)

Code 11:

for i in range(1200,2200):

if(i%5==0 and i%7==0 ):

print("the no. divisible by 7 and are multiple of 5 :",i)

else:

next
Code12:

num=int(input("enter the number :"))

for i in range(2,num):

if (num % i) == 0:

print(num,"is not a prime number")

print(i,"times",num//i,"is",num)

break

else:

print(num,"is a prime number")

break
Code 13:

import pandas as pd

marks = pd.read_csv("[Link]")

print("first 10 rows")

print([Link](10))

Code14:

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

b = int(input("enter the number"))

x, y = a, b

while y:

x, y = y, x % y

hcf = x

lcm = (a * b) // hcf

print("HCF of ",a,"and ",b," is:" ,hcf)

print("LCM of ",a,"and ",b," is:" ,lcm)


Code15:

x_axis=[2,9,8,5,6]

y_axis=[5,10,3,7,18]

import [Link] as plt

[Link](x_axis,y_axis, color='red', marker='s')

[Link]("x")

[Link]("y")

[Link]()

Code 16:

import [Link] as plt


x = list(range(2, 10))

# Line equation: slope = (10-5)/(9-2) = 5/7

y = [5 + (5/7)*(i - 2) for i in x]

[Link](x, y, marker='o')

[Link]("X-axis")

[Link]("Y-axis")

[Link]("Perfectly Straight Line from (2,5) to (9,10)")

[Link](True)

[Link]()

Code 17:

import [Link] as plt

import [Link] as mpimg

image = [Link](r"C:\Users\Manan\Downloads\[Link]")

[Link](image)

[Link]("off") # hides axis

[Link]()
Code 18:

import [Link] as plt

import [Link] as mpimg

image = [Link](r"C:\Users\Manan\Downloads\[Link]")

[Link](image)

[Link]()

print("Image shape:", [Link])


Code 19:

a = [3, 6, 9, 10, 11, 14, 15, 22, 25]

for i in a:

print(i)

Code 20:

name = input("Enter your name: ")

age = int(input("Enter your age: "))

basic_salary = float(input("Enter your basic salary: $"))

da = 0.10 * basic_salary

hra = 0.10 * basic_salary

total_salary = basic_salary + da + hra

print("\n--- Employee Details ---")

print("Name :", name)

print("Age :", age)

print("Basic Salary :$", basic_salary)

print("DA (10%) :$", da)

print("HRA (10%) :$", hra)

print("Total Salary :$", total_salary)

You might also like