Python Numpy Array Exercises

1. Write a Python Numpy program to generate 10 numbers in array a then update fifth number to value 8.

The output is:

Array a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Expected Answer:
Initial array: [ 1 2 3 4 5 6 7 8 9 10]
Update fifth value to 8:
[ 1 2 3 4 8 6 7 8 9 10]

Click for the answer



2. Write a Python Numpy program to generate 10 integers then change the integer type to float type.

The output is:

Array a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Expected Answer:
Initial array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Converted to float type: [ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.]

Click for the answer



3. Write a Python Numpy program to input a list of number then generate one dimensional numpy array as output.

The output is:

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

Expected Answer:
a: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Class of a is [class 'list']
b: [ 1 2 3 4 5 6 7 8 9 10]
Class of b is [class 'numpy.ndarray']

Click for the answer



4. Write a Python Numpy program to generate a numpy array in between number of 2 to 22.

The output is:

Expected Answer:
Numpy array: [ 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22]

Click for the answer



5. Write a Python Numpy program to generate a 4 x 3 matrix with value in between 1 to 12.

The output is:

Expected Answer:
The 4x3 matrix:
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]

Click for the answer



6. Write a Python Numpy program to generate a numpy array in range of 1 to 10 therefore reverse the array.

The output is:

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

Expected Answer:
Original array:
[ 1 2 3 4 5 6 7 8 9 10]

Reverse array:
[10 9 8 7 6 5 4 3 2 1]

Click for the answer



7. Write a Python Numpy program to generate a 3x3 matrix with all number 1, then surround the boarder of 3x3 matrix with number 0.

The output is:

3x3 matrix:
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]

Expected Answer:
3x3 matrix:
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]

Surronding with 0:
[[0. 0. 0. 0. 0.]
[0. 1. 1. 1. 0.]
[0. 1. 1. 1. 0.]
[0. 1. 1. 1. 0.]
[0. 0. 0. 0. 0.]]

Click for the answer



8. Write a Python Numpy program to convert both list a and tuple b in the following output to numpy array.

The output is:

list_a = [1, 2, 3, 4, 5, 6, 7, 8]
tuple_b = (1, 2, 3, 4, 5, 6, 7, 8)

Expected Answer:
List to array:
[1 2 3 4 5 6 7 8]

Tuple to array:
[1 2 3 4 5 6 7 8]

Click for the answer



                               


More Exercises:

Python String Exercises
Python List Exercises
Python Library Exercises
Python Sets Exercises
Python Array Exercises
Python Condition Statement Exercises
Python Lambda Exercises
Python Function Exercises
Python File Input Output Exercises
Python Tkinter Exercises


More Numpy Exercises:

Numpy String Exercises
Numpy Integer Exercises
Numpy Array Exercises
Numpy Data Analysis Exercises
Numpy Data Sorting Exercises


More Pandas Exercises:

Pandas Series Exercises


More Tutorials:

Python Installation - Linux (Ubuntu)
Python Installation - Mac OS
Integrated Development Environment - IDE
Python - Basic Variables
Python - Sequence Introduction
Python - Output Formatting
Python - Escape Character
Python - Type Conversion
Python - Numbers
Python – Arithmetic Operators
Python – Assignment Operators
Python – Comparison Operators
Python – Logical Operators
Python – Precedence and Associativity Operators
Python – Bytes Type and Usage
Python – Long & Raw Strings
Python – Concatenate Function
Python – Slice Function
Python – Length and Split Function
Python – Join and Count Function
Python – Find Function
Python – Index Function
Python – Alignment Function
Python – Startswith and Endswith Function
Python – String Case Conversion
Python – Remove Specified Character
Python – Encode and Decode Function
Python – dir and help Function
Python – Input Output Introduction
Python – Basic Operation
Python – Open Function
Python – Read Function
Python – Readline Function
Python – Write Function
Python – Close Function
Python – Seek and Tell Function
Python – Pickle Module
Python - File Input Module and Linecache Module
Python - Pathlib Module
Python - Pathlib Module
Python - os.path Module
Python - fnmatch Module
Python - Tuple Introduction
Python - List Introduction
Python - List Add Element
Python - List Delete Element
Python - List Modification Element
Python - List Find Element
Python - Dictionary Introduction
Python - Dictionary Basic Operation
Python - Dictionary Method Guide
Python - Set Collection
Python - Set Basic Operation
Python - Set Basic Method
Python - Frozenset Method
Python - If Condition I
Python - If Condition II
Python - While loop
Python - For loop
Python - Pass Statement
Python - Break Statement
Python - Zip Reverse Function
Python - Function Introduction
Python - Positional Parameters
Python - Key Arguments
Python - None and Return
Python - Variable Scope
Python - Local Function
Python - Closure Method
Python - Lamdba Expression