introduction to python
introduction to python
#introduction to python:
*python is a popular programming language.it was created by guido van Rossum, and released in
1991 at cwi(centrum wiskunde & informatica) netherland.
*python is a dynamic .
#why python:
2.platform independent
4.interpreted(byte-code-compiled)
Use: web frameworks and application,gui-based desktop app, ml, data science
Print(“hello , ”+ name)
Print(type(name))
Output:
Hello, Aman
<class ‘str’>
2.num=(input(“enter a number”))
Print (num)
Print(type(num))
Num=num+2
Print(num)
Output:
Enter a number 10
Unit 3 data science
10
<Class ‘str’>
#Type casting
2.num=int((input(“enter a number”)))
Print (num)
Print(type(num))
Num=num+2
Print(num)
Output:
Enter a number 10
10
<Class ‘int’>
12
#variable in python:
Variable name must start with a letter or the underscore character. variable name cannot start with a
number. Variable name can only contain alpha-numeric characters and undescores(A-z,0_9,and).
Ex : var1=5
Var_1=5
_var1=5
Str dict
Bool set
Unit 3 data science
7.binary type:
Bytes ,bytearray
8.none type.
Only read
#numpy stands for numerical python powerful python library that is widely used for scientific
computing, data analysis, and numerical computing task.
.arrays are used to store homogeneous data elements in a contiguous block of memory.
1.Import numpy as np
List1=[10,20,30,40,50]
Array1=np.array(List1)
Print(Array1)
Type(Array1)
Output:
[10 20 30 40 50]
Numpy.ndarrray
2.import numpy as np
List1=[[10,20,30,4],[40,50,60],[70,80,90]]
Array1=np.array(List1)
Print(Array1)
Output:
[[10 20 30]
[40 50 60]
[70 80 90]]
3.Import numpy as np
Unit 3 data science
Array1=np.arange(1,8)
Print(Array1)
Output
[1 2 3 4 5 6 7]
4.Import numpy as np
Array1=np.arange(11,17).reshape((3,2))
Print(Array1)
output
[[11 12]
[13 14]
[15 16]]
5. import numpy as np
Array1=np.zeroes(4)
Print (Array1)
Output:[ 0. 0. 0. 0.]
5. import numpy as np
Array1=np.ones(4)
Print (Array1)
Output:[ 1. 1. 1. 1.]
1.ndim
2.shape
3.size
4.dtype
5.itemsize
List1=[10,20,30,40,50]
Array1=np.array(List1)
Array1.ndim
Array1.shape
Array1.size
Unit 3 data science
Array1.dtype
Array1.itemsize
Output:
(5,)
Dtype(‘int32’)
4
2.import numpy as np
List1=[[10,20,30],[40,50,60],[70,80,90]]
Array1=np.array(List1)
Array1.ndim
Array1.shape
Array1.size
Array1.dtype
Array1.itemsize
Output:
(3,3)
3.import numpy as np
Array3=np.array([[[1,2,3],[4,5,6]],
[[7,8,9],[10,11,12]]])
Print(Array3)
Array3.dim
Array3.shape
Array3.size
Array3.dtype
Array3.itemsize
Output
[[[1 2 3]
Unit 3 data science
[4 5 6]]
[[7 8 9]
[10 11 12]]]
(2,2,3)
12
Import numpy as np
Array1=np.array([10,20,30,40,50])
Print(Array1[0])
Print(Array1[-1])
Output;
10
50
2.array1=np.array([[10,20,30],[40,50,60],[70,80,90]])
Print(array1[1,2])
Print(array1[0,:])
Print(array1[:,1])
Output:
60
10 20 30
20 50 80
Import numpy as np
Array1=np.array([10,20,30,40,50,60,70])
Print(Array1[1:3])
Print(Array1[1:6:2])
Print(Array1[:: 2])
Unit 3 data science
Print(Array1[: : -1])
Output:
20 30
20 40 60
70 60
10 30 50 70
70 60 50 40 30 20 10
Import numpy as np
Array1=np.array([[15,16,17],[25,26,27],[35,36,37],[45,46,47]])
Print(Array1[1,])
Print(Array1[:,1])
Print(Array1[1:3,1;3])
Print(Array1[1:3,])
Print(Array1[:,1:3])
Print(Array1[1:3,1])
Print(Array1[1;3,:1])
Print(Array1[1:3,1:])
0 1 2
0 15 16 17
1 25 26 27
2 35 36 37
3 45 46 47
visualization
Output;
25 26 27
16 26 36 46
26 27 36 37
25 26 27 35 36 37
16 17 26 27 36 37 46 47
26 36
Unit 3 data science
25 35
26 27 36 37
#arithmetuc operations
1.addition(+),-,*,/,//,**,%
Import numpy as np
X=np.array([[1,2],
[3,4]])
Y=np.array([[11,12],
[13,14]])
Z=x-y
Z=x%y // remainder
Print(x.transpose())
Print(z)
#shorting
import numpy as np
x=np.array([[12,11,15],
[21,25,20],
[18,27,16]])
Y=np.sort(x,axis=0)//columns wise
Print(y)
In place;
x.sort()
print(x)
#short in 1d
Import numpy as np
Unit 3 data science
X=np.array([7,2,3,9,6])
1.Y=np.sort(x)
Print(x)
2.Y=np.argsort(x)
Print(y)
3.x.sort(x)
Print(x)
//Y=np.mean(x)
Print(y)
#statical operation
7.var() 8std()
Every dataframe contains rows and columns,and therefore has both a row and column index.
Import pandas as pd
St_data=(1,”varun”30,”male”,”chandigarh”),
(2,”ravi”,31,”male”,”delhi”),
(3,”Preeti”29,”female’,”Jaipur”),
(4,”amrit”32,”male”,”Mumbai”),
(5,”pinki”,28,”female”,”banglore”)]
Df=pd.DataFrame(std_data,columns=[‘stu_id’,’name’,’age’,’gender’,’address’])
df
2.import pandas as pd
df=pd.read_csv(“student.csv”)
df
#df.size //
Unit 3 data science
#df.dtypes
#df.values
#df.index
#df[‘age’]
#df[[‘name’,’address’]]
#def.loc[0]
#def.loc[[0,2,4]]
#def.iloc[0]
#def.iloc[[0,2]]
#filtering rows
df[df[‘age’]>29]
df[‘phone_no’]=[10,20,30,40,50]
df.insert(3,’phone_no’,[10,20,30,40,50]
print(df)
df=df.drop(columns=[‘phone_no’])
df
df=df.drop(columns=[‘phone_no’])
df
#df=df.rename(columns={‘old_name’:’new_name’})
Unit 3 data science
df=df.rename(columns={‘age’: ‘student_age’})
df
del df[phone_no’]
df=df.drop(4)
df
Df.loc[4]=[5,’pinki’,28,’female’,’banglore’]
Df.loc[2,’student_age’]=71
Df
Df.loc[[0,2],’adress’]=[andaman’,’nicobar’]