PRAC-1 - Jupyter Notebook
PRAC-1 - Jupyter Notebook
SESSION-1
NUMBERS:
In [1]: # Addition
6 + 2
Out[1]: 8
In [2]: # Subtraction
6 - 2
Out[2]: 4
In [3]: # Multiplication
6 * 2
Out[3]: 12
In [4]: # Division
6 / 2
Out[4]: 3.0
Out[5]: 4
Out[6]: 36
In [7]: # BODMAS
int((9 + 4) - 5 * 3 + (8 / 2) + (9 - 2))
Out[7]: 9
localhost:8888/notebooks/Documents/DataScienceFolders/PythonBasics/Home/PRAC-1.ipynb 1/29
3/27/24, 7:43 PM PRAC-1 - Jupyter Notebook
Out[8]: 25
VARIABLES:
In [9]: x = 7
y = 8.5
x + y
Out[9]: 15.5
In [10]: # Dtype of x
x,type(x),y,type(y)
In [11]: # Dtype of x + y
type(x + y)
Out[11]: float
STRINGS:
Strings are Immutable.
In [13]: # Dtype of z
type(z)
Out[13]: str
In [14]: # Length of z
len(z)
Out[14]: 14
localhost:8888/notebooks/Documents/DataScienceFolders/PythonBasics/Home/PRAC-1.ipynb 2/29
3/27/24, 7:43 PM PRAC-1 - Jupyter Notebook
Out[15]: 'r'
Out[16]: 'k'
Out[18]: 'Tush'
In [19]: # Slicing x to y
z[5:11] # 5 included but 11 not included.
In [20]: # Slicing with step count [1 to 9 - 10 not included and 2 is the step count
z[1:10:2]
Out[20]: 'uhrSe'
In [21]: # Capitalize
# Capitalizes the first letter of first word in the string.
program = 'data science'
program.capitalize()
In [22]: # Title
# Capitalizes the first letter of all the words in the string.
program.title()
localhost:8888/notebooks/Documents/DataScienceFolders/PythonBasics/Home/PRAC-1.ipynb 3/29
3/27/24, 7:43 PM PRAC-1 - Jupyter Notebook
Out[23]: 1
Out[24]: 6
In [25]: # Upper Function: It UpperCases all the letters in the string for all the w
# Lower Function: It LowerCases all the letters in the string for all the w
program.upper()
In [26]: # Split function: It seperates the words and store them as an item in the l
program.split()
In [27]: # Split function: It seperates the words from the given * char * and store
course = "Python-for-Chitkara"
course.split('-')
Enter Number: 15
Out[28]: str
localhost:8888/notebooks/Documents/DataScienceFolders/PythonBasics/Home/PRAC-1.ipynb 4/29
3/27/24, 7:43 PM PRAC-1 - Jupyter Notebook
--------------------------------------------------------------------------
-
TypeError Traceback (most recent call las
t)
Cell In[29], line 4
1 # Strings are Immutable.
3 subject = 'Python'
----> 4 subject[0] ='B'
5 subject
DATA STRUCTURES:
TUPLES:
In [30]: t1 = (2,4,'Data',6,8,10,'Python')
t1
Out[31]: 8
Out[32]: ('Data', 6, 8)
Out[33]: 4
localhost:8888/notebooks/Documents/DataScienceFolders/PythonBasics/Home/PRAC-1.ipynb 5/29
3/27/24, 7:43 PM PRAC-1 - Jupyter Notebook
Out[34]: 14
Out[35]: 7
Out[36]: 9
Out[37]: 1
--------------------------------------------------------------------------
-
TypeError Traceback (most recent call las
t)
Cell In[38], line 3
1 # Tuples are Immutable.
----> 3 t1[2] = 8
LISTS:
In [39]: l1 = [2,4,6,8,10]
l1
localhost:8888/notebooks/Documents/DataScienceFolders/PythonBasics/Home/PRAC-1.ipynb 6/29
3/27/24, 7:43 PM PRAC-1 - Jupyter Notebook
Out[40]: 6
In [42]: # Extend:
l2 = [9,7,5,3,1]
l2.extend(l1)
l2
Out[47]: [10, 8, 6, 4, 2]
localhost:8888/notebooks/Documents/DataScienceFolders/PythonBasics/Home/PRAC-1.ipynb 7/29
3/27/24, 7:43 PM PRAC-1 - Jupyter Notebook
Out[48]: [10, 8, 6, 4, 2]
Out[49]: [12, 8, 6, 4, 2]
Out[50]: [12, 8, 6, 4, 2]
Out[51]: [12, 8, 6, 4, 2]
Out[52]: [20, 8, 6, 4, 2]
In [53]: # The Value does not change in the original list as seen in Deep Copy.
l1
Out[53]: [12, 8, 6, 4, 2]
Out[54]: []
DICTIONARY:
localhost:8888/notebooks/Documents/DataScienceFolders/PythonBasics/Home/PRAC-1.ipynb 8/29
3/27/24, 7:43 PM PRAC-1 - Jupyter Notebook
Out[56]: dict
Out[57]: 'Tushar'
In [63]: d1.update(d2)
d1
localhost:8888/notebooks/Documents/DataScienceFolders/PythonBasics/Home/PRAC-1.ipynb 9/29
3/27/24, 7:43 PM PRAC-1 - Jupyter Notebook
SETS:
Out[65]: set
Out[66]: {1, 2, 3, 4, 5, 6, 7, 8, 9}
Out[67]: set
CONTROL STATEMENTS:
In [68]: # If-Else [Age-Voting Question]
age = int(input("Enter your Age: "))
if age >= 18:
print('You have the Right to Vote.')
else:
print("Sorry you can't vote.")
localhost:8888/notebooks/Documents/DataScienceFolders/PythonBasics/Home/PRAC-1.ipynb 10/29
3/27/24, 7:43 PM PRAC-1 - Jupyter Notebook
In [70]: # Nested if
num1 = int(input('Enter frist number: '))
num2 = int(input('Enter second number: '))
num3 = int(input('Enter third number: '))
if num1 > num2:
if num1 > num3:
print(num1,' is greater than ', num2, ' and ',num3)
if num2 > num1:
if num2 > num3:
print(num2,' is greater than ', num1, ' and ',num3)
if num3 > num1:
if num3 > num2:
print(num3,' is greater than ', num1, ' and ',num2)
localhost:8888/notebooks/Documents/DataScienceFolders/PythonBasics/Home/PRAC-1.ipynb 11/29
3/27/24, 7:43 PM PRAC-1 - Jupyter Notebook
*
**
***
****
1
22
333
4444
1
12
123
1234
1
2
3
4
5
localhost:8888/notebooks/Documents/DataScienceFolders/PythonBasics/Home/PRAC-1.ipynb 12/29
3/27/24, 7:43 PM PRAC-1 - Jupyter Notebook
In [77]: # Break
l1 = ['Shekhar',2,4,'Data',6,8,10,'Python',56,48,20,54,'Tushar']
for itr in l1:
if itr == 'Python':
break;
print(itr)
Shekhar
2
4
Data
6
8
10
In [78]: # Continue
l1 = ['Shekhar',2,4,'Data',6,8,10,'Python',56,48,20,54,'Tushar']
for itr in l1:
if itr == 'Data':
continue;
print(itr)
Shekhar
2
4
6
8
10
Python
56
48
20
54
Tushar
localhost:8888/notebooks/Documents/DataScienceFolders/PythonBasics/Home/PRAC-1.ipynb 13/29
3/27/24, 7:43 PM PRAC-1 - Jupyter Notebook
Alphabets = 25
Digits = 15
Symbols = 7
FUNCTIONS:
localhost:8888/notebooks/Documents/DataScienceFolders/PythonBasics/Home/PRAC-1.ipynb 14/29
3/27/24, 7:43 PM PRAC-1 - Jupyter Notebook
name_greet('Tushar Shekhar')
multiply(20,3)
60
multiply(20,3) + 10
# To solve this we can use return instead of print.
60
--------------------------------------------------------------------------
-
TypeError Traceback (most recent call las
t)
Cell In[85], line 8
5 def multiply(n1,n2):
6 print(n1 * n2)
----> 8 multiply(20,3) + 10
localhost:8888/notebooks/Documents/DataScienceFolders/PythonBasics/Home/PRAC-1.ipynb 15/29
3/27/24, 7:43 PM PRAC-1 - Jupyter Notebook
multiply(20,3) + 10
Out[86]: 70
student(22,'Tushar Shekhar')
# Here the values are incorrect as we called the function with positional a
Name: 22
Age: Tushar Shekhar
Out[89]: 50
localhost:8888/notebooks/Documents/DataScienceFolders/PythonBasics/Home/PRAC-1.ipynb 16/29
3/27/24, 7:43 PM PRAC-1 - Jupyter Notebook
prime(37)
37 is a prime number.
Out[92]: 98642
scope()
print('Outside function = ',num)
Inside function = 15
Outside function = 10
localhost:8888/notebooks/Documents/DataScienceFolders/PythonBasics/Home/PRAC-1.ipynb 17/29
3/27/24, 7:43 PM PRAC-1 - Jupyter Notebook
factorial(5)
Out[95]: 120
# print series:
num = int(input('Enter number: '))
for i in range(num):
print(fibo(i))
Enter number: 8
0
1
1
2
3
5
8
13
Out[97]: 11
localhost:8888/notebooks/Documents/DataScienceFolders/PythonBasics/Home/PRAC-1.ipynb 18/29
3/27/24, 7:43 PM PRAC-1 - Jupyter Notebook
In [98]: # Subtraction
sub = lambda a,b: a - b
sub(15,6)
Out[98]: 9
In [99]: # Division
div = lambda a,b: a / b
div(54,6)
Out[99]: 9.0
In [100]: # Multiplication
mul = lambda a,b: a * b
mul(5,6)
Out[100]: 30
In [101]: # Square
sqr = lambda a: a ** 2
sqr(5)
Out[101]: 25
In [102]: # Cube
cube = lambda a: a ** 3
cube(5)
Out[102]: 125
localhost:8888/notebooks/Documents/DataScienceFolders/PythonBasics/Home/PRAC-1.ipynb 19/29
3/27/24, 7:43 PM PRAC-1 - Jupyter Notebook
Out[104]: ['It',
'is',
'a',
'long',
'fact',
'that',
'a',
'will',
'be',
'by',
'the',
'of',
'a',
'page',
'when',
'at',
'its']
Out[105]: 46
localhost:8888/notebooks/Documents/DataScienceFolders/PythonBasics/Home/PRAC-1.ipynb 20/29
3/27/24, 7:43 PM PRAC-1 - Jupyter Notebook
List Comprehension
In [108]: # Square of numbers in the list using list comprehension method.
list1 = [n*n for n in [6, 3, 2, 5, 4, 7]]
list1
In [109]: # Uppercasing all the letters in the string using list comprehension method
list2 = [s.upper() for s in 'TusharShekhar']
list2
Out[109]: ['T', 'U', 'S', 'H', 'A', 'R', 'S', 'H', 'E', 'K', 'H', 'A', 'R']
In [110]: # Print only vowels and * for constants in the string using list comprehens
str_name = 'TusharShekhar'
res = [i if i in 'aeiou' else '*' for i in str_name]
res
Out[110]: ['*', 'u', '*', '*', 'a', '*', '*', '*', 'e', '*', '*', 'a', '*']
In [111]: # Print only +ve and * for -ve in the tuple using list comprehension method
tuple1 = (2, 4, -1, 8, 9, -5, 10, 12, -12)
result = [i if i > 0 else '*' for i in tuple1]
result
In [112]: # Print only +ve and the ones divisible by 2 and * for rest in the tuple us
tuple1 = (2, 4, -1, 8, 9, -5, 10, 12, -12)
result = [i if i > 0 else '*' for i in tuple1 if i % 2 ==0]
result
Dictionary Comprehension
In [113]: # Print number with their square using dictionary comprehension method.
dict1 = {d:d*d for d in range(1,11)}
dict1
Out[113]: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100}
localhost:8888/notebooks/Documents/DataScienceFolders/PythonBasics/Home/PRAC-1.ipynb 21/29
3/27/24, 7:43 PM PRAC-1 - Jupyter Notebook
In [114]: # Print number with their string with their length using dictionary compreh
strings = ['Tushar','Shekhar','Introduction','To','Python']
dict2 = {i:len(i) for i in strings}
dict2
In [115]: # Write a program to remove the item present at index 4 and add it to the 2
list1 = [54, 44, 27, 79, 91, 41]
remindx = list1.pop(4)
list1.insert(1,remindx)
list1.append(remindx)
list1
Positive Tweets:
['This government policies are good', 'The way he played showed that he is
one of the best players in the world', 'Her acting in the play was awesom
e', "It's nice to hear this little kid's laugh"]
Negative Tweets:
['bad implementation', 'The wine tastes awful']
localhost:8888/notebooks/Documents/DataScienceFolders/PythonBasics/Home/PRAC-1.ipynb 22/29
3/27/24, 7:43 PM PRAC-1 - Jupyter Notebook
Out[118]: [1, 2, 4, 5, 0, 3, 6, 7, 8]
Numpy
In [120]: import numpy as np
import warnings
warnings.filterwarnings('ignore')
In [123]: type(array1)
Out[123]: numpy.ndarray
In [124]: array1.shape
Out[124]: (6,)
In [125]: array1.ndim
Out[125]: 1
In [127]: array2.shape
Out[127]: (3, 3)
localhost:8888/notebooks/Documents/DataScienceFolders/PythonBasics/Home/PRAC-1.ipynb 23/29
3/27/24, 7:43 PM PRAC-1 - Jupyter Notebook
In [128]: array2.ndim
Out[128]: 2
In [129]: np.arange(2,22,2)
In [130]: np.arange(2,22,2.8)
In [131]: # Linspace
np.linspace(2,22,12)
localhost:8888/notebooks/Documents/DataScienceFolders/PythonBasics/Home/PRAC-1.ipynb 24/29
3/27/24, 7:43 PM PRAC-1 - Jupyter Notebook
In [140]: # Generates random like [0.__] but only +ve numbers only.
np.random.rand(3,3)
Masking in Numpy
In [153]: matrix.shape
Out[153]: (3, 4)
localhost:8888/notebooks/Documents/DataScienceFolders/PythonBasics/Home/PRAC-1.ipynb 25/29
3/27/24, 7:43 PM PRAC-1 - Jupyter Notebook
In [151]: matrix.reshape(4,3)
Broadcasting
In [155]: z1 = np.zeros((3,3))
z1
In [156]: z2 = z1 + 10
z2
localhost:8888/notebooks/Documents/DataScienceFolders/PythonBasics/Home/PRAC-1.ipynb 26/29
3/27/24, 7:43 PM PRAC-1 - Jupyter Notebook
In [164]: nm = np.random.randint(22,55,(3,3))
nm
In [165]: np.argmin(nm)
Out[165]: 8
In [166]: np.argmax(nm)
Out[166]: 1
In [167]: # Mean
ar = np.array([2,4,5,6,8,9,1,3,7])
np.mean(ar)
Out[167]: 5.0
In [168]: # Variance
np.var(ar)
Out[168]: 6.666666666666667
Out[169]: 2.581988897471611
In [170]: # Square
np.square(ar)
In [171]: # Power
np.power(ar,2)
In [173]: # Concat
ap = np.array([2,5,4,6,8,3])
newar = np.concatenate([ar,ap])
newar
localhost:8888/notebooks/Documents/DataScienceFolders/PythonBasics/Home/PRAC-1.ipynb 27/29
3/27/24, 7:43 PM PRAC-1 - Jupyter Notebook
9
8
7
9
8
7
1
2
5
9
8
7
9
8
7
1
2
5
localhost:8888/notebooks/Documents/DataScienceFolders/PythonBasics/Home/PRAC-1.ipynb 28/29
3/27/24, 7:43 PM PRAC-1 - Jupyter Notebook
In [ ]:
localhost:8888/notebooks/Documents/DataScienceFolders/PythonBasics/Home/PRAC-1.ipynb 29/29