Python Programming Basics
Python Programming Basics
Introduction
Python is a high-level, interpreted programming language known for its
simplicity and versatility. It is widely used in web development, data
science, artificial intelligence, automation, and more.
Key Features of Python
Easy to Learn and Use: Simple syntax similar to English.
Interpreted Language: Code is executed line by line.
Dynamically Typed: No need to declare variable types.
Extensive Libraries: Supports multiple domains (e.g., NumPy,
Pandas, Django).
Cross-Platform Compatibility: Runs on Windows, macOS, and
Linux.
Python Basics
Variables and Data Types
Python supports various data types:
Integers: Whole numbers (e.g., x = 10)
Floats: Decimal numbers (e.g., y = 10.5)
Strings: Text (e.g., name = "Python")
Booleans: True or False values (e.g., is_active = True)
Basic Operations
Python supports arithmetic operations:
x = 10
y=5
sum = x + y # Addition
product = x * y # Multiplication
quotient = x / y # Division
Control Flow
Conditional Statements
Python uses if, elif, and else for decision-making:
x = 10
if x > 5:
print("x is greater than 5")
elif x == 5:
print("x is 5")
else:
print("x is less than 5")
Loops
Python has for and while loops:
# For loop
for i in range(5):
print(i)
# While loop
x=0
while x < 5:
print(x)
x += 1
Functions and Modules
Defining Functions
Functions allow code reuse:
def greet(name):
return "Hello, " + name
print(greet("Alice"))
Importing Modules
Python has built-in and external modules:
import math
print(math.sqrt(16))
Conclusion
Python is a powerful and beginner-friendly language that supports
multiple applications. Mastering its basics opens doors to automation,
web development, and data science.