Introduction to Python Programming

Definition Python a high-level interpreted programming language known for its readability and versatility. It supports multiple programming paradigms, including procedural object-oriented, and functional.

Example: A simple program to print "Hello, World!" is:

print("Hello, World!")

Explanation

1. Data and Variables

  • Data Types: Python supports various data types, including:

    • Integers: Whole numbers (e.g., 5, -3)
    • Flo: Decimal numbers (e.g., 3.14, 0.001) : (.g., "", 'Python')
    • oleans: True or False values (e.g., True, False)
  • **Variables Used to store data values. You can create a variable by assigning a value```python age =25 # Integer height = 5.9 # Float name = "Alice" String is_student = # Boolean


### 2. Control Structures
-If Statements:** Used for conditional execution.
```python
if age >= :
    print("Adultelse:
    print("Minor")
  • Loops: for repeated execution - For Loop: pythonfor in(5): print(i) # Prints numbers 0 to 4
 - **While Loop:**
```python
 = 0
while count < 5:
    print(count)
    count += 1

3. Functions and Modules

  • Functions: Blocks of reusable. python def(name): return fHello, {name}print(greet("Alice")) # Output: Hello Alice!

- **Modules:** Files Python code that can be imported and in Python programs.
```pythonimport math
print(math.sqrt()) # Output: 40

4. Introduction to NumPy Arrays

**NumPy A for computing Python It support for arrays and matrices.

import numpy as np
array = np.array([1, 2, ])
print(array)  # Output: [1 2 3]

5. Basic Operations with NumPy

  • Array Operations: You can perform-wise operations on NumPy arrays.
result array1 + array2  # Output: [5 7 9]

Master This Topic with PrepAI

Transform your learning with AI-powered tools designed to help you excel.

6 Introduction to Py Tensors

PyTorch: A library for machine learning that uses tensors, which are similar to NumPy arrays but optimized for GPU.

import
tensor = torch.tensor([1, 2, 3])
print(tensor)  # Output: tensor([, 2, 3])

7. Basic Operations with PyTorch

  • Tensor Operations: Similar to NumPy, you can perform operations on tensors.
tensor1 torch([1, 2 3])
tensor2 = torch([4, 5, 6])
result = tensor1 tensor2  # Output: tensor([5 7, 9])

Real-World Applications

-Data:** Python is widely used for data analysis and visualization.

  • **Web Development Framework like Django and Flask are built on Python.
  • Machine Learning: Libraries like TensorFlow and PyTorch are essential for building ML models.
  • Automation: Python scripts can automate repetitive tasks.

Challenges & Best Practices

  • Common Pit: Forgetting to import libraries, using incorrect data types, and syntax errors-Best Practices:** Write clean readable code; use comments; and follow naming conventions.

Practice Problems Bite-Sized Exercises

  1. Create a variable temperature and assign it a float value. Print it.
  2. Write an statement to check if a number is even or odd. . Use a for loop to print numbers from 1 to .

Advanced Problem

Write a function that takes a list of numbers and a NumPy array with the squares those numbers. Then, use PyTorch to create a tensor from that array.

 square_numbers(numbers):
    import numpy as np
    return np.array([x**2 for x in numbers])

 =1, 2, 3 ]
quared_array = square_numbers(numbers)
(squared_array)  Output: [ 1  4  9 ]

import torch
squared_tensor = torch.tensor(squared_arrayprint(squared_tensor) # Output:([ 1,  4  9, 16])

YouTube References

To enhance your understanding, for the following terms on Pro School's YouTube:

  • "Introduction to Ivy Pro School"
  • "Python Control Structures Ivy Pro"
  • "Py Basics Ivy Pro School"
  • "Py Basics Ivy Pro School"

Reflection

  • What challenges did you face while learning Python and its libraries?
  • How can you apply Python in your current or future projects? What aspects of Python programming do you find most interesting or useful?

Summary- Python is a versatile programming language various data types and structures.

  • Control structures like if statements and loops allow for decision-making and repetition. Functions and modules promote code reusability.
  • NumPy and PyTorch powerful libraries for numerical computing and machine learning.
  • Practice key to mastering Python programming.