0% found this document useful (0 votes)
20 views4 pages

Assignment - 1.ipynb - Colaboratory

The document provides examples of basic operations in Python, NumPy, and Pandas. It demonstrates how to split and format strings, access elements in nested dictionaries, create arrays and matrices, concatenate arrays, generate date ranges, and convert lists to dataframes. Key functions and methods illustrated include split(), format(), np.zeros(), np.full(), np.arange(), np.reshape(), np.concatenate(), pd.DataFrame(), and pd.date_range().

Uploaded by

Tishbian Meshach
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
20 views4 pages

Assignment - 1.ipynb - Colaboratory

The document provides examples of basic operations in Python, NumPy, and Pandas. It demonstrates how to split and format strings, access elements in nested dictionaries, create arrays and matrices, concatenate arrays, generate date ranges, and convert lists to dataframes. Key functions and methods illustrated include split(), format(), np.zeros(), np.full(), np.arange(), np.reshape(), np.concatenate(), pd.DataFrame(), and pd.date_range().

Uploaded by

Tishbian Meshach
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 4

Basic Python

## 1. Split this string

1. Split this string

s = "Hi there Sam!"

splitted_string=s.split()
print(splitted_string)

['Hi', 'there', 'Sam!']

2. Use .format() to print the following string.

Output should be: The diameter of Earth is 12742 kilometers.

planet = "Earth"
diameter = 12742

output_string = "The diameter of {} is {} kilometers.".format(planet, diameter)


print(output_string)

The diameter of Earth is 12742 kilometers.

3. In this nest dictionary grab the word "hello"

d = {'k1':[1,2,3,{'tricky':['oh','man','inception',{'target':[1,2,3,'hello']}]}]}

hello_word= d['k1'][3]['tricky'][3]['target'][3]
print(hello_word)

hello

Numpy
import numpy as np

4.1 Create an array of 10 zeros?

4.2 Create an array of 10 fives?

zeros_array = np.zeros(10)

print(zeros_array)

[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]

array_of_fives = np.full(10, 5)

print(array_of_fives)

[5 5 5 5 5 5 5 5 5 5]

5. Create an array of all the even integers from 20 to 35

even_numbers = np.arange(20, 36, 2)

print(even_numbers)

[20 22 24 26 28 30 32 34]

6. Create a 3x3 matrix with values ranging from 0 to 8

matrix = np.arange(9).reshape(3, 3)

print(matrix)

[[0 1 2]
[3 4 5]
[6 7 8]]

7. Concatenate a and b

a = np.array([1, 2, 3]), b = np.array([4, 5, 6])

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
result = np.concatenate((a, b))
print(result)

[1 2 3 4 5 6]

Pandas

8. Create a dataframe with 3 rows and 2 columns

import pandas as pd

Data= { 'column1':[0,1,2] ,'column': [4,5,6]}


df=pd.DataFrame(Data)
print(df)

column1 column
0 0 4
1 1 5
2 2 6

9. Generate the series of dates from 1st Jan, 2023 to 10th Feb, 2023

start_date = "2023-01-01"
end_date = "2023-02-10"

date_range = pd.date_range(start=start_date, end=end_date)


print(date_range)

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-01-11', '2023-01-12',
'2023-01-13', '2023-01-14', '2023-01-15', '2023-01-16',
'2023-01-17', '2023-01-18', '2023-01-19', '2023-01-20',
'2023-01-21', '2023-01-22', '2023-01-23', '2023-01-24',
'2023-01-25', '2023-01-26', '2023-01-27', '2023-01-28',
'2023-01-29', '2023-01-30', '2023-01-31', '2023-02-01',
'2023-02-02', '2023-02-03', '2023-02-04', '2023-02-05',
'2023-02-06', '2023-02-07', '2023-02-08', '2023-02-09',
'2023-02-10'],
dtype='datetime64[ns]', freq='D')

10. Create 2D list to DataFrame


lists = [[1, 'aaa', 22], [2, 'bbb', 25], [3, 'ccc', 24]]

lists = [[1, 'aaa', 22], [2, 'bbb', 25], [3, 'ccc', 24]]

columns = ['ID', 'Name', 'Age']

df = pd.DataFrame(lists, columns=columns)

print(df)

ID Name Age
0 1 aaa 22
1 2 bbb 25
2 3 ccc 24

You might also like