Introduction to Python
Python is a high-level, interpreted programming language known for its simplicity and readability. It is used for web development, data analysis, artificial intelligence, and scientific computing.
Note: Python is dynamically typed, which means you don't need to declare variable types explicitly.
Basic Syntax
Here’s an example of a simple Python program:
# This is a comment
print("Hello, World!")
Example: The above code will output
Hello, World!
to the console.
Variables in Python
Variables in Python are created when you assign a value to them. You don't need to declare their type explicitly.
x = 5 # An integer
y = "Bookphilic" # A string
print(x)
print(y)
Data Types in Python
Python has several built-in data types:
- Numbers: int, float, complex
- Strings: str
- Lists: list
- Tuples: tuple
- Dictionaries: dict
- Sets: set
Conditional Statements
Conditional statements in Python are used to perform different actions based on conditions:
x = 10
if x > 5:
print("x is greater than 5")
elif x == 5:
print("x is equal to 5")
else:
print("x is less than 5")
Loops in Python
Python supports two types of loops:
- for loops
- while loops
# Example of a for loop
for i in range(5):
print(i)