PYTHON
PYTHON
In a world teeming with information, the ability to transform raw data into
meaningful insights is a skill highly sought after. Python, a versatile
programming language, has emerged as the go-to tool for data scientists.
Python's strength in data science comes from its powerful libraries. Get to
know the following key libraries:
Random: For generating random test cases while training and testing of
data.
DATATYPES
OPERATORS
TYPECASTING
Implicit Typecasting
Explicit typecasting
Implicit Typecasting
float_number = 3.14
result = integer_number + float_number
Explicit Typecasting
Int():
Python Int() function take float or string as an argument and returns int
type object.
float():
Python float() function take int or string as an argument and return float
type object.
str():
Python str() function takes float or int as an argument and returns string
type object.
STRING
A String is a data structure in Python that represents a sequence of
characters. It is an immutable data type, meaning that once you have
created a string, you cannot change it. Strings are used widely in many
different applications, such as storing and manipulating text data,
representing names, addresses, and other types of data that can be
represented as text.
String Concatenation
string1 = "Python"
print(concatenated_string)
Output
Data Science Python
String Replication
original_string = "Python"
replicated_string = original_string * 3
print(replicated_string)
Output – PythonPythonPython
String Slicing
Note - One thing to keep in mind while using this method is that the
string returned after slicing includes the character at the start index but
not the character at the last index.
# Creating a String
String1 = "GeeksForGeeks"
print(String1[5:12])
print(String1[3:-3])
Output - Slicing characters from 5-12:
ForGeek
ksForGe
uppercase_string = string.upper()
lowercase_string = string.lower()
print(uppercase_string)
print(lowercase_string)
Output - HELLO, PYTHON!
hello, python!
String Stripping
stripped_string = whitespace_string.strip()
print(stripped_string)
l_stripped_string = whitespace_string.lstrip()
print(l_stripped_string)
r_stripped_string = whitespace_string.rstrip()
print(r_stripped_string)
Output - Python
Python
Python
String Replacing
print(new_string)
Output - I am experienced in Python
String Count
count = string.count("python")
print(count)
Output - 3
String Find
The find() method returns the index of the first occurrence of a substring.
If not found, it returns -1.
index = string.find("Python")
print(index)
index = string.find("python")
print(index)
Output - 0
-1
String Formatting
Strings in Python can be formatted with the use of format() method which
is a very versatile and powerful tool for formatting Strings. Format method
in String contains curly braces {} as placeholders which can hold
arguments according to position or keyword to specify the order.
# Default order
print(String1)
# Positional Formatting
print(String1)
# Keyword Formatting
print(String1)
Output - Print String in default order: Begineer in Python
# --------------------------------------- STRINGS
------------------------------------------
#String replication
print('Vishal'*4)
print('-'*20)
#String length
print(len('pallavishal'))
#String slicing
print('vishalpalla'[0])
print('vishalpalla'[2])
print('vishalpalla'[0:6])
print('vishalpalla'[6:])
print('vishalpalla'[6:len('vishalpalla')+1])
print('vishal'[:6])
#string reverse
a = 'vishal'
print(a[::-1])
#String striping
print(' vishal '.strip())
#String replace
print('vishalpalla'.replace('palla',''))
#String count
print('vishalpalla'.count('a'))
#String find
print('vishalpalla'.find('palla'))
#String check
print('vishalpalla'.islower())
print('vishalpalla'.isupper())
print('vishalpalla'.isalpha())
print('vishalpalla'.isnumeric())
#String Capitalization
print('vishal palla'.capitalize())
print('vishal palla'.title())
print('-'*20)
# :: usage in string
b = 'gayathri'
print(b[::-1])
print(b[::-2])
print(a[::-1])
print(a[::-2])
IF-ELSE
if(n ==0):
print('It is a zero')
elif (n >= 0):
print('Number is a positive number')
else:
print('Number is a negative number')
FOR LOOP
for i in range(11):
for j in range(11):
print(i, j)
#--------------------------------------------------------------
word = "hello"
for letter in word:
print(letter.upper())
#--------------------------------------------------------------
numbers = [1, 2, 3, 4, 5, 6]
for num in numbers:
if num % 2 == 0:
print(f"{num} is even")
else:
print(f"{num} is odd")
#--------------------------------------------------------------
names = ["Alice", "Bob", "Charlie"]
for index, name in enumerate(names):
print(f"{index + 1}: {name}")
#--------------------------------------------------------------
print(flat_list)
#--------------------------------------------------------------
result = {}
for i in range(len(keys)):
result[keys[i]] = values[i]
print(result)
While Loop
n = 10
s = 0
i =0
while i <= n:
s+=i
i +=1
print(s)
# Reverse a number
n =876545
temp = n
reverse = ''
while temp >0:
r = temp%10
reverse = reverse + str(r)
temp = temp//10
print(reverse)
LIST
Python Lists are just like the arrays, declared in other languages which is
an ordered collection of data. It is very flexible as the items in a list do not
need to be of the same type. Here are the key characteristics of lists:
print(lst)
# adding a element
lst.append('vishal')
print(lst)
#slicing
print(lst[1:4])
print(lst[::-1]) #reversed a list
# :: usage in list
b = 'gayathri'
print(lst[::-1])
print(lst[::-2])
print(lst[::2])
#removing
lst.remove('Ankur')
print(lst)
#length
print(len(lst))
#sorting
print(lst)
print(sorted(lst))
#reverse
print(lst)
print('reverse a list')
lst.reverse()
print(lst)
lst = ['Ashish', 'Ankur', 'Riya', 'Adarsh', 'Parag', 'Avneet', 'vishal']
#Find a element
print(lst.index('vishal'))
#Count
print(lst.count('vishal'))
#Extending
lst.extend(['arpan', 'shubhendu', 'Harsh'])
print(lst)
for i in lst:
print(i, end=' ')
print()
print('-'*20)
for i in range(len(lst)):
print(lst[i], end=' ')
print()
print('-'*20)
# iterating the list from reverse order...
for i in range(len(lst)-1, -1, -1):
print(lst[i], end=' ')
print()
print('-' * 20)
print(lst[::-1])
List Comprehensions
Dictionary
Dictionary Syntax
Properties of dictionary
for i in dct.keys():
print(i , dct[i])
print('-'*50)
print(dct.items())
print('-'*50)
for i in dct.items():
print(i)
print('-'*50)
dct_1.update(dct_2)
print(dct_1)
print(dct_2)
print('-'*50)
Dictionary Comprehension
print('-'*30)
Example of a Set:
my_set = {1, 2, 3, 4}
my_set.add(5) # Adding an element
print(my_set) # Output: {1, 2, 3, 4, 5}
Example of a Tuple:
my_tuple = (1, 2, 3, 4)
print(my_tuple[0]) # Accessing the first element: Output: 1
Modules
def find_average(numbers):
if not numbers:
return 0
def filter_even(numbers):
def reverse_list(input_list):
return input_list[::-1]
Importing Module
import list_operations
print(avg,evens)
print(evens)
import list_operations as op
print(reverse)
__name__:
This variable is set to "__main__" when the module is run directly, and it is
set to the module's name when imported. It allows code to be executed
conditionally.
def find_average(numbers):
if not numbers:
return 0
def reverse_list(numbers):
return numbers[::-1]
def filter_even(numbers):
return [num for num in numbers if num % 2 == 0]
if __name__ == "__main__":
# Code here will only run when the module is executed directly
print(reverse_list([2,4,5,7,9])
For example:
my_package/
|-- __init__.py
|-- list_operations.py
|-- string_operations.py
Best Practices
Choose clear and descriptive names for modules and functions to enhance
code readability.
3. Versioning
Function
def greet():
print('Hello world')
greet()
# Passing parameters
def greet(n=1):
print('Number : ', n)
greet(10)
greet(n=5)
greet()
# return
def sum(a,b):
return a+b
print(sum(1,2))
#multi returns
def arthamatics(a,b):
return a+b, a-b, a*b;
CLASS
class Agent:
def __init__(self, name, age):
self.name = name
self.age = age
self.health = 100
self.isAlive = True
def punched(self):
self.health -= 10
def shot(self):
self.health -= 30
def info(self):
if self.health > 0:
self.isAlive = True
print('Name: ', self.name)
print('Age: ', self.age)
print('Health: ', self.health)
print('IsAlive: ', self.isAlive)
LIBRARIES
# MATH
import math
x = 10.8
print(math.ceil(x))
print(math.floor(x))
print(math.trunc(x))
print('-'*30)
x = 3
print(math.exp(x))
print(math.log10(x))
print('-'*30)
x= 90
print(math.sin(x))
print(math.cos(x))
print(math.tan(x))
print('-'*30)
print(math.pi)
print(math.e)
print('10 Factorial: ', math.factorial(10))
print('--------'*30)
#Random
import random
print(random.random())
print(random.randint(1,11))
print(random.choice([1,2,3,4,5]))
print(random.sample([1,2,3,4,5],2))
print(random.uniform(1.0,3.0))
print('--------'*30)
# datetime
import datetime
print(datetime.datetime.now())
print(datetime.datetime(2024,12,29,5,5))
print(datetime.datetime.now().strftime("%d/%m/%y %H:%M:%S"))
print('--------'*30)
# Collections
lst = [1,2,3,4,5,5,5,6,6,7]
print(Counter(lst))
print('-'*30)
d = defaultdict(int)
d['a'] += 2
print(d)
print('-'*30)
d = OrderedDict()
d['one'] = 1
d['two'] = 2
print(d)
print('-'*30)
#STRING
import string
print(string.ascii_letters)
print(string.ascii_lowercase)
print(string.ascii_uppercase)
print('-'*30)
print(string.digits)
print(string.hexdigits)
print(string.octdigits)
print('-'*30)
print(string.punctuation)
IMPORTING IN PYTHON
import math
from collections import Counter, defaultdict, OrderedDict
from Support import greet, sum
greet()
addition = sum(10,20)
print(addition)
print(list(zip(lst_1, lst_2)))
print('-'*80)
lst_1 = [2, 4, 6]
lst_2 = [1, 3, 5]
LAMBDA
#LAMBDA
add_num = lambda x,y : x+y
print(add_num(10,20))
num = [1,2,3,4,5,6,7,8,9,10]
FILTER
lst = [1,2,3,4,5,6,7,8,9]
def is_even(n):
return n%2 ==0
MAP
# Sqaure the elements in the list
num = [1,2,3,4,5,6,7,8,9,10]
#Map
#Approach 1
def square(n):
return n**2
print(list(map(square, num)))
#Approach 2
print(list(map(lambda x : x**2, num))) # Map and Lambda
TRY EXCEPT
try:
num1 = 10
num2 = 20
print(num1/num2)
except ZeroDivisionError as zde:
print(zde)
except:
print('error occured')
else:
print('Gayathri')
finally:
print('Vishal')
Anaconda
• What is it?
Anaconda is a distribution of Python and R programming languages designed for
scientific computing, data analysis, machine learning, and large-scale data
processing. It simplifies package management and deployment.
• Key Features:
2. Pre-installed Libraries: Includes over 1,500 libraries for data science, such
as NumPy, pandas, matplotlib, and TensorFlow.
• Usage: Ideal for professionals in data science, AI, and researchers working on
machine learning projects.
Jupyter
• What is it?
Jupyter is an open-source web-based application that lets you create and share
documents containing live code, equations, visualizations, and narrative text. It is
part of the Project Jupyter.
• Key Features:
2. Multiple Languages: Primarily supports Python but also works with R, Julia,
and other languages via kernels.
• Usage: Widely used for teaching, research, and sharing results in fields like data
science, AI, and computational science.
Anaconda comes pre-installed with Jupyter Notebook, making it easy to start using
Jupyter without separately installing its dependencies. This combination is highly
recommended for beginners in data science or programming.
Read data from a text file..
fd = open('data.txt')
txt = fd.read()
print(txt)
fd.close()
‘w’ -> it will create a file, if didn’t exist and write/overwrite into the file
‘a’ -> it will create a file and append the text to it.
fd = open('datav2.txt', 'w')
fd.write('vishal palla loves gayathri')
fd.close()
fd = open('datav2.txt', 'a')
fd.write('and gayathri loves vishal palla')
fd.close()
import json
fd = open('records.json')
js = fd.read()
fd.close()
records = json.loads(js)
for i in records:
print(i['id'], i['product_name'])
js = json.dumps(records)
fd = open('records.json', 'w')
fd.write(js)
fd.close()
NUMPY
We are going to use google colab for workspace and practice the code.
# to install numpy
!pip install numpy
import numpy as np
lst = [[1,2,3],[4,5,6],[7,8,9]]
print(type(lst))
arr = np.array(lst)
print(type(arr))
print(arr)
Arithmetic Operators
import numpy as np
# create an array
arr = np.array([1,2,3,4,5,6,7,8])
Array Sorting
Array Merging