Skip to content

ComputerScienceAndEngineering/python-lab

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

Python Programs


1. Sum of all even numbers from 1 to 50

Code

even_sum = 0

for i in range(1, 51):
    if i % 2 == 0:
        even_sum += i

print("Sum of even numbers from 1 to 50:", even_sum)

Output

Sum of even numbers from 1 to 50: 650

2. Display all prime numbers between 20 and 50

Code

print("Prime numbers between 20 and 50:")

for num in range(20, 51):
    if num > 1:
        is_prime = True
        for i in range(2, num):
            if num % i == 0:
                is_prime = False
                break
        if is_prime:
            print(num)

Output

Prime numbers between 20 and 50:
23
29
31
37
41
43
47

3. Find the length of a string without using library functions

Code

string = "Hello World"
count = 0

for char in string:
    count += 1

print("Length of the string:", count)

Output

Length of the string: 11

4. Perform operations on a list (add, insert, slicing)

Code

my_list = [10, 20, 30, 40]

my_list.append(50)
print("After add:", my_list)

my_list.insert(2, 25)
print("After insert:", my_list)

slice_list = my_list[1:4]
print("Sliced list:", slice_list)

Output

After add: [10, 20, 30, 40, 50]
After insert: [10, 20, 25, 30, 40, 50]
Sliced list: [20, 25, 30]

5. Create a one-dimensional NumPy array and display shape & datatype

Code

import numpy as np

arr = np.array([1, 2, 3, 4, 5])

print("Array:", arr)
print("Shape:", arr.shape)
print("Data type:", arr.dtype)

Output

Array: [1 2 3 4 5]
Shape: (5,)
Data type: int64

6. Perform statistical operations using NumPy

Code

import numpy as np

data = np.array([10, 20, 30, 40, 50])

print("Mean:", np.mean(data))
print("Median:", np.median(data))
print("Standard Deviation:", np.std(data))

Output

Mean: 30.0
Median: 30.0
Standard Deviation: 14.1421356237

7. Load dataset from CSV using pandas

Code

import pandas as pd

df = pd.read_csv("data.csv")
print(df.head())

Output

   hours_studied  attendance_percent  exam_score
0            7.0                88.0         79.0
1            4.0                78.0         59.0
2            8.0                64.0         72.0
3            5.0                92.0         71.0
4            3.0                70.0         50.0

8. Implement Linear Regression and predict output

Code

from sklearn.linear_model import LinearRegression
import numpy as np

X = np.array([[1], [2], [3], [4], [5]])
y = np.array([2, 4, 6, 8, 10])

model = LinearRegression()
model.fit(X, y)

prediction = model.predict([[6]])
print("Predicted output:", prediction[0])

Output

Predicted output: 12.0

9. Check whether a dataset contains missing values

Code

import pandas as pd

df = pd.read_csv("data.csv")
print(df.isnull().sum())

Output

hours_studied          6
attendance_percent     6
exam_score             6

10. Split dataset into training and testing sets

Code

from sklearn.model_selection import train_test_split
import pandas as pd

data = pd.read_csv("data.csv")
data = data.fillna(data.mean())

X = data[["hours_studied", "attendance_percent"]]
y = data["exam_score"]

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)

print(X_train.shape)
print(X_test.shape)

Output

(48, 2)
(12, 2)

📌 End

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors