Introduction To Python
Introduction To Python
Introduction to Python:
Python is a high-level, interpreted, and general-purpose programming language.
It's known for its readability and simplicity.
Python is dynamically typed (no need to declare variable types) and supports
object-oriented, procedural, and functional programming paradigms.
2. Basic Syntax:
Variables: No need to declare types.
python
Copy code
x = 10
name = "Sumit"
Comments: Single-line (#) and multi-line comments ("""...""").
Indentation: Indentation is used to define code blocks (functions, loops,
conditions).
3. Data Types:
Numbers: int, float, complex.
Strings: Defined using single or double quotes.
python
Copy code
s = "Hello"
Booleans: True or False.
Lists: Ordered, mutable collections.
python
Copy code
my_list = [1, 2, 3]
Tuples: Ordered, immutable collections.
python
Copy code
my_tuple = (1, 2, 3)
Dictionaries: Unordered collections of key-value pairs.
python
Copy code
my_dict = {'name': 'Sumit', 'age': 22}
4. Control Flow:
If-Else Statements:
python
Copy code
if x > 5:
print("Greater than 5")
else:
print("Less or equal to 5")
Loops:
for loop:
python
Copy code
for i in range(5):
print(i)
while loop:
python
Copy code
while x < 10:
print(x)
x += 1
5. Functions:
Defined using def keyword.
python
Copy code
def greet(name):
return f"Hello, {name}"
Can have default parameters.
Supports recursion and higher-order functions (like lambda functions).
6. Object-Oriented Programming (OOP):
Python supports classes and objects.
Class and Object:
python
Copy code
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
return f"Hi, I am {self.name}"
p = Person("Sumit", 22)
print(p.greet())
7. Libraries and Modules:
You can import pre-built libraries like math, random, or external ones using pip.
python
Copy code
import math
print(math.sqrt(16))
8. File Handling:
Reading and writing to files using open().
python
Copy code
with open("file.txt", "r") as file:
content = file.read()
9. Exception Handling:
Use try-except blocks to handle errors.
python
Copy code
try:
x = 1 / 0
except ZeroDivisionError:
print("Cannot divide by zero")
10. Libraries for Data Science and Web Development:
Data Science: pandas, numpy, matplotlib, scikit-learn.
Web Development: Django, Flask.