0
0
Data Analysis Pythondata~10 mins

Array creation (array, arange, linspace) in Data Analysis Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Array creation (array, arange, linspace)
Start
Choose method
array()
Create array
Output numpy array
End
You start by choosing one of three methods to create a numpy array: array() for fixed lists, arange() for ranges with steps, or linspace() for evenly spaced numbers. Each creates an array output.
Execution Sample
Data Analysis Python
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.arange(0, 5, 2)
arr3 = np.linspace(0, 1, 5)
This code creates three arrays: a fixed list, a range with step 2, and 5 evenly spaced numbers between 0 and 1.
Execution Table
StepActionInput ParametersResulting ArrayNotes
1Call np.array()[1, 2, 3][1 2 3]Creates array from list
2Call np.arange()start=0, stop=5, step=2[0 2 4]Creates array from range with step
3Call np.linspace()start=0, stop=1, num=5[0. 0.25 0.5 0.75 1. ]Creates array with evenly spaced values
4End--All arrays created successfully
💡 All arrays created; no errors; execution ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3
arr1undefined[1 2 3][1 2 3][1 2 3]
arr2undefinedundefined[0 2 4][0 2 4]
arr3undefinedundefinedundefined[0. 0.25 0.5 0.75 1. ]
Key Moments - 3 Insights
Why does np.arange(0, 5, 2) not include 5 in the output?
Because np.arange stops before the stop value; it includes numbers less than 5 but not equal to 5, as shown in step 2 of the execution table.
How is np.linspace different from np.arange when creating arrays?
np.linspace creates a fixed number of evenly spaced values between start and stop (inclusive), while np.arange creates values with a fixed step size but may not include the stop value, as seen in steps 2 and 3.
What type of input does np.array() accept?
np.array() accepts a list or sequence of numbers and creates an array exactly matching those values, as shown in step 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of arr2 after step 2?
A[0 2 4 6]
B[0 2 4]
C[0 1 2 3 4]
D[1 2 3]
💡 Hint
Check the 'Resulting Array' column for step 2 in the execution table.
At which step does the array contain evenly spaced decimal values?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look for decimal numbers in the 'Resulting Array' column.
If np.arange(0, 6, 2) was used instead of np.arange(0, 5, 2), what would be the last element in arr2?
A6
B5
C4
D8
💡 Hint
np.arange stops before the stop value, so it will not include 6.
Concept Snapshot
Array creation methods in numpy:
- np.array(list): creates array from given list
- np.arange(start, stop, step): creates array from start to stop (exclusive) with step
- np.linspace(start, stop, num): creates num evenly spaced values from start to stop (inclusive)
Use these to quickly build arrays for data analysis.
Full Transcript
This lesson shows how to create arrays in numpy using three methods: array(), arange(), and linspace(). The array() method takes a list and makes an array with the same values. The arange() method creates numbers from start up to but not including stop, stepping by a given amount. The linspace() method creates a set number of evenly spaced values between start and stop, including both ends. We traced code that creates three arrays using these methods and saw their outputs. We also answered common questions about why arange excludes the stop value and how linspace differs by spacing values evenly. This helps beginners understand how to build arrays for data work.