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

Python Programming Basics and Examples

The document contains various code snippets demonstrating basic programming concepts in Python, including checking if a number is even or odd, finding the largest of three numbers, and printing a range of numbers. It also covers operations using NumPy and Pandas for data manipulation, as well as creating visualizations with Matplotlib. Each code snippet is accompanied by its expected output.

Uploaded by

r3709708
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Python Programming Basics and Examples

The document contains various code snippets demonstrating basic programming concepts in Python, including checking if a number is even or odd, finding the largest of three numbers, and printing a range of numbers. It also covers operations using NumPy and Pandas for data manipulation, as well as creating visualizations with Matplotlib. Each code snippet is accompanied by its expected output.

Uploaded by

r3709708
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Program: Check Even or Odd


Code:
n=7
if n % 2 == 0:
print("Even")
else:
print("Odd")
Output: Odd

2. Largest of Three Numbers


Code:
a, b, c = 12, 5, 20
print(max(a, b, c))
Output: 20

3. Print 1 to 10 Using Loop


Code:
for i in range(1, 11):
print(i)
Output: 1 2 3 4 5 6 7 8 9 10

4. Factorial Using Loop


Code:
n=5
fact = 1
for i in range(1, n+1):
fact *= i
print(fact)
Output: 120

5. Function to Add Two Numbers


Code:
def add(a, b):
return a + b
print(add(10, 20))
Output: 30

6. NumPy Array Basic Operations


Code:
import numpy as np
arr = [Link]([10, 20, 30, 40])
print([Link](), [Link](), [Link]())
Output: 25.0 40 10

7. NumPy Indexing and Slicing


Code:
arr = [Link]([5, 10, 15, 20, 25])
print(arr[1], arr[1:4])
Output: 10 [10 15 20]

8. Create Pandas Series


Code:
import pandas as pd
s = [Link]([10, 20, 30])
print(s)
Output:
0 10
1 20
2 30

9. Pandas DataFrame from Dictionary


Code:
data = {'Name':['A','B'], 'Marks':[90,85]}
df = [Link](data)
print(df)
Output:
Name Marks
0 A 90
1 B 85

10. Handle Missing Data


Code:
df = [Link]({'A':[1, None, 3], 'B':[4, 5, None]})
print([Link](0))
Output:
AB
014
105
230

11. Load CSV and Show Head


Code:
df = pd.read_csv('[Link]')
print([Link]())
Output: (Sample first 5 rows)

12. Sort & Filter DataFrame


Code:
df_sorted = df.sort_values('Marks')
print(df_sorted[df_sorted['Marks'] > 50])
Output: (Filtered rows)

13. Line Chart Using Matplotlib


Code:
import [Link] as plt
x = [1,2,3]
y = [2,4,6]
[Link](x,y)
[Link]()

14. Bar Chart for Students' Marks


Code:
names = ['A','B','C']
marks = [80, 90, 70]
[Link](names, marks)
[Link]()
15. Pie Chart for Subject Distribution
Code:
subjects = ['Math','Sci','Eng']
sizes = [40, 30, 30]
[Link](sizes, labels=subjects)
[Link]()

You might also like