U21EC082 - Jinhal Maheshwari
In [1]: #Aim : Study basic operations in Python programming and NumPy library.
In [2]: # Data_Types_and_Basics.ipynb
# Importing libraries for later sections
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.linear_model import LinearRegression, LogisticRegression
from sklearn.cluster import KMeans
# Title and Introduction
print("## Introduction to Python Data Types, Control Structures, File Handling,
# Section 1: Data Types
print("## 1. Data Types")
# Numbers
print("## Numbers")
numl = 10 # Integer
num2 = 20.5 # Float
print("Integer:", numl, "Float:", num2)
# Boolean
print ("## Boolean")
is_active = True
print("Boolean:", is_active)
# Strings
print ("## Strings")
greeting = "Hello, World!"
print("String:", greeting)
# Section 2: Print Command
print("# 2. Print Command")
print("Printing is essential in Python to display outputs")
# Section 3: Data Structures
print("# 3. Data Structures")
# Lists
print("### List")
fruits = ["apple", "banana", "cherry"]
print("List:", fruits)
# Dictionaries
print("### Dictionary")
ages = {"Alice": 25, "Bob": 30}
print("Dictionary:", ages)
# Tuples
print ("Ht Tuple")
dimensions = (1920, 1080)
print("Tuple:", dimensions)
# Sets
print ("#4 Set")
unique_numbers = {1, 2, 3, 4}
print("Set:", unique_numbers)
# Section 4: Control Structures
print("## 4. Control Structures")
# If-Else
print ("## If-Else")
x = 10
if x>5:
print("x is greater than 5")
else:
print("x is 5 or less")
# For Loop
print("## For Loop")
for fruit in fruits:
print("Fruit:", fruit)
# While Loop
print ("## While Loop")
counter = 3
while counter > 0:
print("Counter:", counter)
counter -= 1
# Section 5: Functions
print("### 5. Functions")
def add_numbers(a, b):
return a +b
print("Function Example - Sum:", add_numbers(5, 10))
# Section 6: File Handling
print("## 6. File Handling")
with open("example.txt", "w") as f:
f.write("Hello, File!")
with open("example.txt", "r") as f:
content = f.read()
print("File Content:", content)
# Section 7: NumPy
print("# 7. NumPy")
# Random
print ("## Random")
random_numbers = np.random.rand(5)
print("Random Numbers:", random_numbers)
# Array Methods
print("### Array Methods")
array = np.array([1, 2, 3, 4, 5])
print("Sum:", array.sum())
# Indexing and Broadcasting
print("### Indexing and Broadcasting")
print("Element at index 2:", array[2])
# 1D and 2D Arrays
print("### 1D and 2D Arrays")
array_1d = np.array([1, 2, 3])
array_2d = np.array([[1, 2], [3, 4]])
print("1D Array:", array_1d, "2D Array:", array_2d)
# Logical Operator to Filter
print("### Logical Operator to Filter")
print("Filtered Array:", array[array > 2])
# Operators and Linear Algebra
print("### Operators and Linear Algebra")
dot_product = np.dot(array_1d, array_1d)
print("Dot Product:", dot_product)
## Introduction to Python Data Types, Control Structures, File Handling, and Libr
aries
## 1. Data Types
## Numbers
Integer: 10 Float: 20.5
## Boolean
Boolean: True
## Strings
String: Hello, World!
# 2. Print Command
Printing is essential in Python to display outputs
# 3. Data Structures
### List
List: ['apple', 'banana', 'cherry']
### Dictionary
Dictionary: {'Alice': 25, 'Bob': 30}
Ht Tuple
Tuple: (1920, 1080)
#4 Set
Set: {1, 2, 3, 4}
## 4. Control Structures
## If-Else
x is greater than 5
## For Loop
Fruit: apple
Fruit: banana
Fruit: cherry
## While Loop
Counter: 3
Counter: 2
Counter: 1
### 5. Functions
Function Example - Sum: 15
## 6. File Handling
File Content: Hello, File!
# 7. NumPy
## Random
Random Numbers: [0.91871481 0.64182313 0.50477278 0.99124674 0.02954423]
### Array Methods
Sum: 15
### Indexing and Broadcasting
Element at index 2: 3
### 1D and 2D Arrays
1D Array: [1 2 3] 2D Array: [[1 2]
[3 4]]
### Logical Operator to Filter
Filtered Array: [3 4 5]
### Operators and Linear Algebra
Dot Product: 14