First NumPy Program
In this chapter, we'll write our first NumPy program and experience NumPy's powerful features through practical code examples.
Hello NumPy
Let's start with the simplest NumPy program:
Output:
Basic Array Operations
Creating Different Types of Arrays
Output:
Using Built-in Functions to Create Arrays
Output:
Basic Array Properties
Output:
Basic Mathematical Operations
Array and Scalar Operations
Output:
Array and Array Operations
Output:
Array Indexing and Slicing
1D Array Indexing
Output:
2D Array Indexing
Output:
Common Mathematical Functions
Output:
Practical Examples
Example 1: Student Grade Statistics
Output:
Example 2: Simple Data Analysis
Output:
Common Errors and Considerations
1. Import Alias
2. Array Dimensions
3. Data Types
Chapter Summary
In this chapter, we learned:
- How to create our first NumPy program
- Basic array creation methods
- Basic array properties and operations
- Array indexing and slicing
- Basic mathematical operations and functions
- Practical application examples
- Common errors and considerations
Next Steps
In the next chapter, we'll dive deeper into NumPy's fundamental concepts, including array internal structure, memory layout, and more advanced topics.
Exercises
- Create an array containing numbers 1 to 20, calculate its squares and cubes
- Create a 3x3 random array, find the positions of maximum and minimum values
- Create two arrays and calculate their dot product
- Simulate rolling a die 1000 times, count the frequency of each outcome
- Create a temperature conversion program that converts Celsius to Fahrenheit