Welcome to the Python Basics section of the Data Science and AI Roadmap! Python is the most widely used programming language in the world of Data Science and AI due to its simplicity and powerful libraries. In this section, we will cover the fundamentals of Python that will serve as the building blocks for more advanced topics later on.
- Why Python for Data Science?
- Setting Up Python
- Basic Syntax and Variables
- Data Types and Structures
- Control Flow
- Functions and Modules
- File Handling
- Python Libraries for Data Science
- Exercises
Python is the go-to language for Data Science due to several reasons:
- Simplicity: Python has a readable and easy-to-learn syntax that is beginner-friendly.
- Vast Libraries: Python provides numerous libraries such as
NumPy,Pandas,Matplotlib, andSeabornthat are crucial for data manipulation and visualization. - Community Support: With an active and large community, Python has vast documentation, tutorials, and libraries that can assist in almost any challenge.
- Integration: Python integrates seamlessly with other programming languages and big data tools, making it versatile.
Before starting with Python, you need to set up the development environment. Here's how:
- Download the latest version of Python from the official site: Python.org.
- During installation, make sure to check the box for "Add Python to PATH."
You can write Python code in any text editor, but it's recommended to use an Integrated Development Environment (IDE) or code editor like:
- VS Code
- Jupyter Notebooks
- PyCharm
Once installed, open a terminal or command prompt and type:
python --version
If the installation was successful, you should see an output like this:
Python 3.x.xPython is known for its simplicity and readability. The syntax of Python is designed to be clean and straightforward, making it an excellent language for beginners while also being powerful for advanced users. Below is an introduction to the basic syntax and variable handling in Python.
Unlike many programming languages that use braces {} to define blocks of code, Python uses indentation (spaces or tabs) to denote block structures. This is a key feature of Python, and it is critical to ensure the correct indentation level, as improper indentation will result in an error.
Example:
if True:
print("This is inside an if block") # This line is indentedPython supports two types of comments:
- Single-line comments start with a #.
- Multi-line comments can be made using triple quotes ''' or """.
Example:
# This is a single-line comment
"""
This is a multi-line comment
spanning multiple lines
"""Variables in Python do not require explicit declaration, and their data types are inferred based on the value assigned. You can assign values to variables and change their types dynamically. ** Rules for Variable Names:**
- Variable names must start with a letter or an underscore (_).
- The rest of the name can contain letters, numbers, or underscores.
- Variable names are case-sensitive (name and Name are different).
Example:
x = 10 # x is an integer
y = "Hello" # y is a string
z = 3.14 # z is a floating-point number
# Variable reassignment
x = "Python" # x is now a stringPython is dynamically typed, meaning you do not need to declare the type of a variable. Python will automatically interpret the variable type based on the assigned value.
- Integers: Whole numbers (e.g., x = 10)
- Floating-point numbers: Decimal numbers (e.g., y = 3.14)
- Strings: Text enclosed in quotes (e.g., z = "Hello")
- Boolean: True or False values (e.g., a = True)
age = 25 # Integer
price = 19.99 # Float
name = "Alice" # String
is_active = True # BooleanPython allows for multiple variables to be assigned in a single line.
Example:
a, b, c = 1, 2, 3
x = y = z = "Same value"Python allows you to convert between types using casting functions:
- int() - Converts to an integer
- float() - Converts to a float
- str() - Converts to a string
- bool() - Converts to a boolean **Example: **
x = int(3.14) # x will be 3
y = float("7.5") # y will be 7.5
z = str(25) # z will be '25'In Python, you can print variables using the print() function. You can also format output using f-strings for easy embedding of variables into strings.
Example:
name = "John"
age = 30
# Simple print
print(name)
# Print with formatting
print(f"My name is {name} and I am {age} years old.")Python has a set of reserved keywords that cannot be used as variable names. These include if, else, for, while, def, return, etc.
Example of Reserved Keywords:
# Don't use these as variable names
if, else, for, while, class, return, True, FalseThis concludes the basics of Python's syntax and variable handling. With these foundational concepts, you can begin exploring more advanced language features.
Python provides several built-in data types and data structures that make it easy to store and manipulate data. Understanding these is crucial for efficient programming. Below is a comprehensive guide to Python's key data types and structures.
Data Types already discussed in above for more you can check phyton doc
Lists are ordered, mutable (changeable) collections of items. Items in a list can be of different data types. Lists are defined using square brackets [].
Example:
numbers = [1, 2, 3, 4, 5]
mixed_list = [1, "hello", True, 3.14]- Accessing elements:
first_element = numbers[0] # 1- Modifying elements:
numbers[2] = 10- List methods:
-
numbers.append(6) # Adds 6 at the end numbers.remove(10) # Removes 10 from the list
### 2. Tuples
Tuples are similar to lists but are immutable (cannot be changed after creation). They are defined using parentheses ().
**Example:**
```python
coordinates = (10, 20)
- Accessing elements:
x = coordinates[0] # 10- Tuples cannot be modified:
# coordinates[0] = 15 -> This will raise an errorDictionaries store data in key-value pairs. They are unordered and mutable. Defined using curly braces {}, where each item is a key-value pair separated by a colon : . Example:
person = {
"name": "Alice",
"age": 30,
"is_student": False
}- Accessing values by key:
name = person["name"] # "Alice"
- Modifying values:
person["age"] = 31
- Adding a new key-value pair:
person["city"] = "New York"
