python
python
1. What is Python?
Python is a high-level, interpreted programming language known for its readability,
simplicity, and versatility. It supports multiple programming paradigms, including
procedural, object-oriented, and functional programming. Python is open-source and
has a large community that contributes to its rich ecosystem of libraries and
frameworks.
3. Installing Python
1. Download Python:
o Visit the official Python website to download the latest version of
Python.
2. Installation:
o Follow the installation instructions for your respective operating
system. Make sure to check the box to add Python to your system
PATH during installation.
3. Verifying Installation:
o Open a command prompt or terminal and type python --
version or python3 --version to verify the installation.
python
Basic Operators:
python
sum_result = my_int + my_float # Addition
difference = my_int - 5 # Subtraction
product = my_int * 2 # Multiplication
quotient = my_float / 2 # Division
Data Structures:
python
python
my_tuple = (1, 2, 3)
python
python
my_set = {1, 2, 3, 4}
my_set.add(5)
5. Control Flow
Conditional Statements:
python
if my_int > 5:
print("Greater than 5")
elif my_int == 5:
print("Equal to 5")
else:
print("Less than 5")
Loops:
python
# For loop
for i in range(5):
print(i)
# While loop
count = 0
while count < 5:
print(count)
count += 1
6. Functions
Defining Functions:
python
def greet(name):
return f"Hello, {name}!"
square = lambda x: x ** 2
print(square(4)) # Output: 16
python
import math
print(math.sqrt(16)) # Output: 4.0
Popular Libraries:
8. File Handling
Reading and Writing Files:
python
# Writing to a file
with open("example.txt", "w") as file:
file.write("Hello, Python!")
9. Error Handling
Try/Except Blocks:
python
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero!")
finally:
print("Execution completed")
python
class Dog:
def __init__(self, name):
self.name = name
def bark(self):
return f"{self.name} says woof!"
my_dog = Dog("Buddy")
print(my_dog.bark()) # Output: Buddy says woof!
Conclusion
Python is a versatile and widely-used programming language with applications in
various domains, from web development to data science and artificial intelligence.
Its simplicity and robust libraries make it a preferred choice for beginners and
professionals alike.