0% found this document useful (0 votes)
11 views31 pages

PYTHON

The document provides an overview of Python as a programming language for data science, highlighting its key libraries such as NumPy, Pandas, and Scikit-Learn. It also covers fundamental programming concepts including typecasting, string manipulation, loops, lists, dictionaries, and sets, along with examples for each. The content is structured to guide readers through the essential features and functionalities of Python relevant to data science and general programming.

Uploaded by

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

PYTHON

The document provides an overview of Python as a programming language for data science, highlighting its key libraries such as NumPy, Pandas, and Scikit-Learn. It also covers fundamental programming concepts including typecasting, string manipulation, loops, lists, dictionaries, and sets, along with examples for each. The content is structured to guide readers through the essential features and functionalities of Python relevant to data science and general programming.

Uploaded by

Palla Vishal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 31

PYTHON

Python for Data Science

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 Libraries for Data Science

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.

NumPy: For numerical operations and working with arrays.

Pandas: Ideal for data manipulation and analysis.

Matplotlib and Seaborn: Used for data visualization.

Scikit-Learn: Essential for machine learning tasks.

DATATYPES
OPERATORS

TYPECASTING

Typecasting, also known as type conversion or coercion, is the process of


converting one data type into another in programming languages. Python
being a dynamically typed language, allows for flexible typecasting to
accommodate various data types.

There are basically two types of Type conversion in python:

 Implicit Typecasting
 Explicit typecasting

Implicit Typecasting

In certain situations python automatically converts one data type to


another when operations involve different data types. This is known as
implicit type conversion.

1. Integer and float


integer_number = 5

float_number = 3.14
result = integer_number + float_number

Explicit Typecasting

Explicit type conversion, also known as typecasting, is a process in which


a programmer manually converts the data type of an object to the desired
data type. In Python, built-in functions such as int(), float(), str(), etc., are
used to perform explicit type conversion.

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

String concatenation involves combining multiple strings into a single


string using the + operator.

string1 = "Python"

string2 = "Data Science"

concatenated_string = string2 + " " + string1

print(concatenated_string)
Output
Data Science Python

String Replication

String replication is achieved by using '*' operator to create a repeated


copies of a string.

original_string = "Python"

replicated_string = original_string * 3

print(replicated_string)
Output – PythonPythonPython

String Slicing

In Python, the String Slicing method is used to access a range of


characters in the String. Slicing in a String is done by using a Slicing
operator, i.e., a colon (:).

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"

# Printing 3rd to 12th character

print("Slicing characters from 5-12: ")

print(String1[5:12])

# Printing between 3rd and 2nd last character

print("Slicing characters between " +


"3rd and 3rd last character: ")

print(String1[3:-3])
Output - Slicing characters from 5-12:

ForGeek

Slicing characters between 3rd and 3rd last character:

ksForGe

String Case Conversion

Strings can be converted to uppercase or lowercase using upper() and


lower() methods.

string = "Hello, Python!"

uppercase_string = string.upper()

lowercase_string = string.lower()

print(uppercase_string)

print(lowercase_string)
Output - HELLO, PYTHON!

hello, python!

String Stripping

Stripping removes leading and trailing whitespace from a string using


strip(), lstrip(), or rstrip().

whitespace_string = " Python "

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

The replace() method replaces occurrences of a substring with another


substring.

string = "I am beginner in Python"

new_string = string.replace("beginner", "experienced")

print(new_string)
Output - I am experienced in Python

String Count

The count() method counts the number of occurrences of a substring in a


string.

string = "python program run on python IDLE, in pythonic way."

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.

string = "Python is powerful and Pythonic."

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

String1 = "{} {} {}".format('Begineer', 'in', 'Python')

print("Print String in default order: ")

print(String1)

# Positional Formatting

String1 = "{1} {0} {2}".format('Begineer', 'in', 'Python')

print("\nPrint String in Positional order: ")

print(String1)

# Keyword Formatting

String1 = "{l} {f} {g}".format(g='Begineer', f='in', l='Python')

print("\nPrint String in order of Keywords: ")

print(String1)
Output - Print String in default order: Begineer in Python

Print String in Positional order: in Begineer Python

Print String in order of Keywords: Python in Begineer

# --------------------------------------- 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 case conversation


print('vishalpalla'.upper())
print('vishalpalla'.lower())

#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())

# String check with startswith and endwith


print('vishal palla'.startswith('vish'))
print('vishal palla'.endswith('alla'))

#string center, ljust, rjust


print('vishal palla'.center(20,'^'))
print('vishal palla'.ljust(20,'*'))
print('vishal palla'.rjust(20,'#'))

print('-'*20)
# :: usage in string

b = 'gayathri'
print(b[::-1])
print(b[::-2])
print(a[::-1])
print(a[::-2])

IF-ELSE

n = int(input('enter the number: '))

if(n ==0):
print('It is a zero')
elif (n >= 0):
print('Number is a positive number')
else:
print('Number is a negative number')

if((n%3==0) and (n%5==0)):


print(n, 'is a divided by 3 and 5')

FOR LOOP

# n = int(input('Enter the number:'))

for i in range(11):
for j in range(11):
print(i, j)
#--------------------------------------------------------------

fruits = ["apple", "banana", "cherry"]


for fruit in fruits:
print(f"I like {fruit}")
#--------------------------------------------------------------

for i in range(1, 6):


print(f"Square of {i} is {i**2}")
#--------------------------------------------------------------

word = "hello"
for letter in word:
print(letter.upper())
#--------------------------------------------------------------

person = {"name": "Alice", "age": 25, "city": "New York"}


for key, value in person.items():
print(f"{key}: {value}")
#--------------------------------------------------------------

for i in range(1, 4):


for j in range(1, 4):
print(f"i = {i}, j = {j}")
#--------------------------------------------------------------

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}")
#--------------------------------------------------------------

for num in range(1, 10):


if num == 5:
print("Skipping 5")
continue
elif num == 8:
print("Stopping the loop")
break
print(num)
#--------------------------------------------------------------

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


flat_list = []
for sublist in nested_list:
for num in sublist:
flat_list.append(num)

print(flat_list)
#--------------------------------------------------------------

keys = ["name", "age", "city"]


values = ["Alice", 25, "New York"]

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:

 Lists in Python maintain the order of elements as they are inserted.


The order in which elements are added is the same order in which
they are stored.
 Lists are mutable, meaning you can modify their elements after the
list is created. You can change, add, or remove elements from a list.
 Lists can contain elements of different data types.
 Lists in Python are dynamic, meaning they can grow or shrink in size
as needed.
 Lists support indexing, allowing you to access individual elements
using their position (index) in the list.
 Lists are iterable, meaning you can loop through each element using
a for loop.

lst = ['Ashish', 'Ankur', 'Riya', 'Adarsh', 'Parag', 'Avneet']

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)

#Finding max and min


print(min(lst))
print(max(lst))
print('-'*20)

#Iterating the list

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

List comprehensions provide a concise way to create lists. They offer a


compact syntax for generating lists based on existing iterables, such as
lists, strings, or ranges.

lst = [X**2 for X in range(1,11)]


print(lst)
print()
lst1 = [x**2 for x in range(1,100) if x%2==0]
print(lst1)

#multi dimensional list


lst3 = [[i for i in range(5)] for j in range(5)]
print(lst3)

Dictionary

A dictionary in Python is a collection of key values, used to store data


values like a map, which, unlike other data types holds Key-value only a
single value as an element.

Dictionary Syntax

dict_var = {key1 : value1, key2 : value2, …..}

Properties of dictionary

 Dictionaries in Python are unordered collections of items. Unlike


lists, the order in which items are added to a dictionary is not
preserved.
 Dictionaries are mutable, meaning you can modify their contents by
adding, removing, or updating key-value pairs.
 Keys in a dictionary must be immutable, meaning they cannot be
changed after creation. Values in a dictionary, on the other hand,
can be of any data type and are mutable.
 Retrieving a value from a dictionary is highly efficient, typically
taking constant time on average.

dct = {1:'vishal', 2: 'Gayathri'}


print(dct)
print('-'*20)
#accessing the elements
print(dct[2])
print(dct.get(1))
print('-'*20)

#Iterating through the Dictionary


print(dct.keys())
print(dct.values())
print('-'*50)

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)

for k,v in dct.items():


print(k,v)
print('-'*50)

#checking a key is present


print(dct)
print(1 in dct)
print(10 in dct)
print('-'*50)

#updating the dictionaries


dct_1={1:'a', 2:'b'}
dct_2 ={1:'c',2:'d'}

dct_1.update(dct_2)
print(dct_1)
print(dct_2)

print('-'*50)

Dictionary Comprehension

dct = { i: i**2 for i in range(1,11) }


print(dct)

print('-'*30)

dct = { i:i**2 for i in range(1,11) if i%2 == 0 }


print(dct)
print('-'*30)

# Dictionaries with list


lst = {'vishal', 'gayathri', 'prabhas'}
dct = { name: len(name) for name in lst }
print(dct)
print('-'*30)
dct = { len(name): name for name in lst }
print(dct)
print('-'*30)

# Special Keys with Strings


dct = { 'num_'+ str(i) :i for i in range(1,101)}
print(dct)
print('-'*30)

#Shortlisting from values


dct = {k:v for k,v in dct.items() if v%3==0 }
print(dct)

lst1 = ['a', 'b', 'c', 'd']


lst2 = [1,2,3,4]

dct = { lst2[i]:lst1[i] for i in range(len(lst2)) }


print(dct)

What is a Set in Python?

A set is an unordered and mutable collection of unique elements. It is useful when


you want to store distinct items and perform operations like union, intersection, and
difference.

Key Features of a Set:

• Unordered: The elements have no specific order.

• Mutable: You can add or remove items.

• Unique: No duplicate elements are allowed.

• Set elements must be hashable (immutable types like integers, strings,


tuples).

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}

What is a Tuple in Python?

A tuple is an ordered and immutable collection of elements. It is used when you


want to group related items together that should not be changed.

Key Features of a Tuple:

• Ordered: The elements have a specific order.


• Immutable: Once created, elements cannot be added, removed, or modified.

• Can contain duplicate elements.

• Elements can be of mixed types.

Example of a Tuple:

my_tuple = (1, 2, 3, 4)
print(my_tuple[0]) # Accessing the first element: Output: 1

Modules

A Module is a file in python containing Python definitions and statements.


These files help organize code into logical, reusable units. Modules make
it easier to manage and scale Python projects by breaking them down into
smaller components.
Creating a Module

A Python module is created by saving Python code in a file with


a extension. For example, let's create a simple module named :

def find_average(numbers):

if not numbers:

return 0

return sum(numbers) / len(numbers)

def filter_even(numbers):

return [num for num in numbers if num % 2 == 0]

def reverse_list(input_list):

return input_list[::-1]

def concatenate_lists(list1, list2):

return list1 + list2

This module, , contains four functions for basic list operations.

Importing Module

Once a module is created, it can be imported into other Python scripts or


modules. There are several ways to import a module:

Importing the Entire Module

import list_operations

avg = list_operations.find_average([5, 3, 4, 7])

evens = list_operations.filter_even([5, 3, 4, 7])

print(avg,evens)

Importing Specific Functions

from list_operations import filter_even


evens = filter_even([3,5,6,7,8,0])

print(evens)

Importing with an Alias

import list_operations as op

reverse = op.reverse_list([5, 2, 7, 6])

print(reverse)

Module Search Path

When importing a module, Python searches for it in a specific order


defined by the **module search path**. The search path includes the
current directory, directories listed in the environment variable, and
default system directories. Understanding the module search path is
crucial for managing dependencies and avoiding naming conflicts.

Special Variables in Modules

Modules have access to some special variables:

__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

return sum(numbers) / len(numbers)

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])

Package: Organizing Modules

A package is a way of organizing related modules into a single directory


hierarchy. A package is essentially a directory containing module files and
a special __init__.py file, which can be empty.

For example:

my_package/

|-- __init__.py

|-- list_operations.py

|-- string_operations.py

In this example, is a package containing two modules.

Best Practices

1. Use Descriptive Names

Choose clear and descriptive names for modules and functions to enhance
code readability.

2. Avoid Global Variables

Minimize the use of global variables within modules to prevent unintended


side effects.

3. Versioning

Consider using version numbers in your module names or directory


structures to manage changes and updates.

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;

sum, sub, multi = arthamatics(10,20)


print(sum)
print(sub)
print(multi)

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

from collections import Counter, defaultdict, OrderedDict

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)

ZIP, LAMBDA, FILTER, MAP


ZIP
lst_1 = ['Ashish', 'Ankur', 'Avinash']
lst_2 = [25,30,32]

print(list(zip(lst_1, lst_2)))
print('-'*80)
lst_1 = [2, 4, 6]
lst_2 = [1, 3, 5]

print(sum([i * j for i, j in zip(lst_1, lst_2)]))

print( [i for i in lst_1])

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]

print(list(filter(lambda x: x%2==0, num)))

FILTER
lst = [1,2,3,4,5,6,7,8,9]

def is_even(n):
return n%2 ==0

res = filter(is_even, lst)


print(list(res))

MAP
# Sqaure the elements in the list
num = [1,2,3,4,5,6,7,8,9,10]

print([ i**2 for i in num ]) # List comprehension

#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 and Jupyter


Anaconda and Jupyter are tools widely used in data science, machine learning, and
programming for managing and analyzing data.

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:

1. Package Management: Comes with conda, a powerful package manager


to easily install, update, or remove libraries.

2. Pre-installed Libraries: Includes over 1,500 libraries for data science, such
as NumPy, pandas, matplotlib, and TensorFlow.

3. Environments: Allows creating isolated environments to manage


dependencies for different projects.
4. GUI Tools: Comes with tools like Anaconda Navigator for managing
projects, environments, and libraries with a graphical interface.

• 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:

1. Interactive Code Execution: Supports running code in real-time, making it


excellent for testing and debugging.

2. Multiple Languages: Primarily supports Python but also works with R, Julia,
and other languages via kernels.

3. Visualization: Allows seamless integration of graphs and plots (e.g., via


matplotlib or seaborn).

4. Notebooks: Documents in the .ipynb format can be shared and reused


easily.

• Usage: Widely used for teaching, research, and sharing results in fields like data
science, AI, and computational science.

Relationship Between Anaconda and Jupyter:

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..

Create a new python code base using jupyter.

fd = open('data.txt')
txt = fd.read()
print(txt)
fd.close()

Write into a file..

‘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()

READ DATA FROM JSON AND WRITE TO JSON

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.

GETTING STARTED WITH NUMPY

# to install numpy
!pip install numpy

import numpy as np

lst = [[1,2,3],[4,5,6],[7,8,9]]
print(type(lst))

#converting a list to arry using numpy

arr = np.array(lst)
print(type(arr))

#checking the dimension of an array


print(arr.ndim)

#checking the shape of an array


print(arr.shape)

#checking the size of an array


print(arr.size)

Reshape and Random Number Generator


import numpy as np

#generates a random number


np.random.random()

#generates an array of size 4,3


arr = np.random.random((4,3))

print(arr)

#generates the array from 1 to 10


np.arange(1,11,1)

#generates the array of 100 from between 1 to 10


np.linspace(1,10,100)
arr

# it reshapes the array but the array size should be same


np.reshape(arr, (3,4))

# it flatten the array


arr.flatten()

Arithmetic Operators
import numpy as np

# create an array
arr = np.array([1,2,3,4,5,6,7,8])

#Add 1 to all elements


print(arr + 1)

#Subtract 1 to all elements


print(arr - 1)

#Multiply 1 to all elements


print(arr * 1)

#Divide 1 to all elements


print(arr / 1)

#Square 2 to all elements


print(arr ** 2)

#Finding max element in array


arr.max()

#Create a multi dimension array


arr = np.array([ [1,2,3], [4,5,6],[7,8,9] ])

#Max element in column


arr.max(axis = 0)

#Max element in row


arr.max(axis = 1)

Array Sorting
Array Merging

Automating Using Numpys

You might also like