Python Fundamentals
• A detailed overview of Python Basics and Core Concepts
1. Introduction to Python
• Python is a high-level, interpreted programming language known for its simplicity
and readability.
• Key Features:
• - Easy to learn and use
• - Extensive standard libraries
• - Cross-platform support
• - Open-source and community-driven
2. Variables and Data Types
• Variables store data values. Example:
• x = 10 # Integer
• y = 3.14 # Float
• Common Data Types:
• - int: Integer values
• - float: Floating-point numbers
• - str: Strings
• - bool: Boolean values
• - list, tuple, set, dict: Data structures
3. Input and Output
• Input: Use input() to take user input.
• Example: name = input('Enter your name: ')
• Output: Use print() to display output.
• Example: print('Welcome to Python!')
4. Operators
• Operators are used to perform operations on variables and values.
• Examples:
• - Arithmetic: +, -, *, /, %
• - Relational: ==, !=, >, <
• - Logical: and, or, not
• - Assignment: =, +=, -=, *=, /=
5. Control Flow (Conditionals and Loops)
• Conditional Statements:
• if, elif, else
• Example:
• if x > 0:
• print('Positive')
• Loops:
• - for: Iterates over a sequence
• - while: Repeats as long as a condition is true
6. Functions
• Functions are blocks of reusable code.
• Example:
• def greet(name):
• print('Hello, ' + name)
• greet('Alice')
7. Data Structures
• Lists: Ordered and mutable
• Tuples: Ordered and immutable
• Sets: Unordered and unique
• Dictionaries: Key-value pairs
8. File Handling
• Python provides built-in functions to work with files:
• - Open a file: open('file.txt', 'r')
• - Read: file.read()
• - Write: file.write('Hello')
• - Close: file.close()
9. Exception Handling
• Handle runtime errors using try-except blocks.
• Example:
• try:
• x = 10 / 0
• except ZeroDivisionError:
• print('Cannot divide by zero')
10. Modules and Libraries
• Modules and libraries help extend Python's capabilities.
• Examples:
• - Math: import math
• - Random: import random
• - OS: import os
• - Requests: import requests