Assignment 1
Assignment 1
Python Programming
Basic Python
1. Split this string
In [ ]:
s = "Hi there Sam!"
In [ ]:
x = s.split() print(x)
['Hi', 'there', 'Sam!']
In [ ]:
planet = "Earth" diameter
= 12742
In [ ]:
print ('The diameter of {} is {} kilometers.' .format(planet,diameter)) The diameter of Earth is 12742
kilometers.
In [ ]:
d = {'k1':[1,2,3,{'tricky':['oh','man','inception',{'target':[1,2,3,'hello']}]}]}
In [ ]:
print(d['k1'][3]['tricky'][3]['target'][3]) hello
Numpy
In [ ]:
import numpy as np
4.1 Create an array of 10 zeros?
In [ ]:
In [ ]:
arr = np.zeros(10)
print (arr)
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] In [ ]:
[5. 5. 5. 5. 5. 5. 5. 5. 5. 5.]
arr = np.ones(10)*5
print (arr)
arr = np.arange(20,35,2)
print ( arr )
[20 22 24 26 28 30 32 34]
In [ ]:
Out[ ]:
array([1, 2, 3, 4, 5, 6])
Pandas
8. Create a dataframe with 3 rows and 2 columns
In [3]:
import pandas as pd
In [10]:
df = pd.DataFrame(data1,data, columns=['numbers']) df Out[10]:
numbers
10 1
20 2
30 3
9. Generate the series of dates from 1st Jan, 2023 to 10th Feb, 2023
In [15]:
pd.date_range(start='1/1/2023', end='10/02/2023') Out[15]:
DatetimeIndex(['2023-01-01', '2023-01-02', '2023-01-03', '2023-01-04',
'2023-01-05', '2023-01-06', '2023-01-07', '2023-01-08', '2023-01-
09', '2023-01-10',
...
'2023-09-23', '2023-09-24', '2023-09-25', '2023-09-26',
'2023-09-27', '2023-09-28', '2023-09-29', '2023-09-30',
'2023-10-01', '2023-10-02'], dtype='datetime64[ns]',
length=275, freq='D')
In [21]:
lists = [[1, 'aaa', 22], [2, 'bbb', 25], [3, 'ccc', 24]]
In [29]:
df = pd.DataFrame(lists, columns =['FName', 'LName', 'Age'], dtype = int) print(df)
FName LName Age 0
1 aaa 22
1 2 bbb 25
2 3 ccc 24
/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py:3326: FutureWarni
ng: Could not cast to int64, falling back to object. This behavior is deprecated. In a fu
ture version, when a dtype is passed to 'DataFrame', either all columns will be cast to t
hat dtype, or a TypeError will be raised exec(code_obj, self.user_global_ns, self.user_ns)
In [ ]: