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

Python

The document provides a comprehensive overview of basic data types in Python, including integers, floats, strings, booleans, and NoneType. It also covers various operations and methods for lists, tuples, dictionaries, and introduces functions for a simple calculator, positive/negative checks, filtering even numbers, and working with dates and strings. Additionally, it includes examples of using NumPy arrays, concatenating DataFrames with pandas, reading CSV files, calculating the area of a circle, and plotting data with matplotlib.

Uploaded by

d10604097
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 views7 pages

Python

The document provides a comprehensive overview of basic data types in Python, including integers, floats, strings, booleans, and NoneType. It also covers various operations and methods for lists, tuples, dictionaries, and introduces functions for a simple calculator, positive/negative checks, filtering even numbers, and working with dates and strings. Additionally, it includes examples of using NumPy arrays, concatenating DataFrames with pandas, reading CSV files, calculating the area of a circle, and plotting data with matplotlib.

Uploaded by

d10604097
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 Basic data types ▶

# Basic Data Types in Python


x = 10 # int
y = 3.14 # float
name = "Python" # str
flag = True # bool
nothing = None # NoneType

print(type(x), type(y), type(name), type(flag), type(nothing))


# <class 'int'> <class 'float'> <class 'str'> <class 'bool'> <class 'NoneType'>

#2List methods ▶
lst = [10, 20, 30]

[Link](1, 15) # insert at index


print(lst) # [10, 15, 20, 30]

[Link](40) # add at end


print(lst) # [10, 15, 20, 30, 40]

[Link](15) # remove by value


print(lst) # [10, 20, 30, 40]

print(len(lst)) # 4

[Link]() # remove last item


print(lst) # [10, 20, 30]

[Link]() # empty the list


print(lst) # []
#3Tuple operations ▶
t = (1, 2, 3)

# Add items (tuples are immutable — convert to list)


t = t + (4, 5)
print(t) # (1, 2, 3, 4, 5)

print(len(t)) # 5

print(3 in t) # True

print(t[0]) # 1 — access by index


print(t[1:3]) # (2, 3) — slicing

#4Dictionary methods ▶
d = {"name": "Alice", "age": 21, "city": "Delhi"}

print([Link]()) # all key-value pairs

print(d["name"]) # Alice — access item

print([Link]("age")) # 21 — safe access

d["age"] = 22 # change value

print(d["age"]) # 22

print(len(d)) # 3

#5Calculator menu with functions ▶


def add(a, b): return a + b
def sub(a, b): return a - b
def mul(a, b): return a * b
def div(a, b): return a / b if b != 0 else "Error"

print("[Link] [Link] [Link] [Link]")


ch = int(input("Choice: "))
a = float(input("Enter a: "))
b = float(input("Enter b: "))

ops = {1: add, 2: sub, 3: mul, 4: div}


print("Result:", ops[ch](a, b) if ch in ops else "Invalid")

#6Positive / negative check ▶


n = float(input("Enter a number: "))

if n > 0:
print("Positive")
elif n < 0:
print("Negative")
else:
print("Zero")

#7Filter even numbers ▶


nums = [1, 2, 3, 4, 5, 6, 7, 8]

evens = list(filter(lambda x: x % 2 == 0, nums))


print(evens) # [2, 4, 6, 8]

#8Print date and time ▶


import datetime

today = [Link]()
now = [Link]()

print("Today:", today) # 2025-08-01

print("Now:", now) # 2025-08-01 14:35:22.123

#9Add days to current date ▶


import datetime

today = [Link]()
n = int(input("Days to add: "))
new_dt = today + [Link](days=n)

print("New date:", new_dt)

#10Count characters in string → dict ▶


s = input("Enter string: ")
freq = {}

for ch in s:
freq[ch] = [Link](ch, 0) + 1

print(freq)
# e.g. {'h':1, 'e':1, 'l':2, 'o':1}

#11Frequency of characters in file ▶


freq = {}

with open("[Link]", "r") as f:


for line in f:
for ch in line:
if ch != "\n":
freq[ch] = [Link](ch, 0) + 1

print(freq)

#12NumPy array properties ▶


import numpy as np

arr = [Link]([[1,2,3],[4,5,6]])

print(type(arr)) # <class '[Link]'>


print([Link]) # axes: 2
print([Link]) # shape: (2, 3)
print([Link]) # element type: int64

#13Concatenate DataFrames ▶
import pandas as pd

df1 = [Link]({"A":[1,2], "B":[3,4]})


df2 = [Link]({"A":[5,6], "B":[7,8]})

result = [Link]([df1, df2], ignore_index=True)


print(result)
#AB
#013
#124
#257
#368

#14Read CSV with Pandas ▶


import pandas as pd

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

print([Link]()) # first 5 rows


print([Link]()) # last 5 rows

#15Area of circle using math module ▶


import math

r = float(input("Enter radius: "))


area = [Link] * r ** 2

print(f"Area = {area:.2f}")

#16CSV line plot with matplotlib ▶


import pandas as pd
import [Link] as plt

df = pd.read_csv("[Link]")
profit = [Link]("Month")["Profit"].sum()

[Link]([Link], [Link],
color="blue", linewidth=2,
marker="o", linestyle="--",
label="Monthly Profit")

[Link]("Total Profit by Month")


[Link]("Month")
[Link]("Profit")
[Link]()
[Link](True)
plt.tight_layout()
[Link]()

You might also like