History: Python was created by Guido van Rossum in 1989 and was released in 1991. It was designed with simplicity and readability in mind, making it easy for new programmers to learn.
Purpose: Python is a high-level, general-purpose programming language used for web development, data analysis, machine learning, artificial intelligence, automation, and much more.
Uses: Python is used across various domains, including web development (Django, Flask), data science (Pandas, NumPy, Matplotlib), machine learning (TensorFlow, Keras), and scripting/automation.
Advantages: Python is easy to read, has rich libraries, is versatile, and open-source.
Interpreted Language: Python is an interpreted language, meaning that the code is executed line-by-line by the Python interpreter rather than being compiled into machine code.
Installing Python: Download Python from the official website python.org and ensure to check "Add Python to PATH" during installation.
Choosing an IDE/Text Editor:
Interactive Shell: Type python
or python3
in your terminal/command prompt to start the interactive shell.
Running Scripts: Save Python code as script_name.py
and run it using the command:
python script_name.py
Statements: Python code is composed of statements.
Indentation: Python uses indentation to define blocks of code. Indentation is crucial and should be consistent (spaces or tabs).
if True:
print("Hello, Python!") # This is correctly indented
Comments: Comments are used to explain code and are ignored by the interpreter. Python supports single-line and multi-line comments:
# This is a single-line comment
"""
This is a multi-line
comment in Python
"""
Integers: Whole numbers without a decimal point.
x = 10 # Integer
Floats: Numbers with a decimal point.
y = 3.14 # Float
Complex Numbers: Numbers with a real and imaginary part.
z = 5 + 2j # Complex Number
Strings can be defined using single or double quotes.
name = 'John'
greeting = "Hello, Python!"
Escape Sequences: Special characters like newline \n
and tab \t
.
message = "Hello\nWorld"
print(message) # Output: Hello\nWorld
Raw Strings: Strings with backslashes treated as literal characters.
path = r"C:\Users\Name\Documents"
Python supports two boolean values: True
and False
.
is_active = True
is_logged_in = False
Python 3 introduced type hints to specify expected data types.
def add(a: int, b: int) -> int:
return a + b
a = 5
b = 2
print(a + b) # Output: 7
print(a ** b) # Output: 25
a = 5
a += 3 # a = a + 3
print(a) # Output: 8
print(5 == 5) # Output: True
print(5 != 5) # Output: False
print(True and False) # Output: False
print(True or False) # Output: True
a = 5 # 0101 in binary
b = 3 # 0011 in binary
print(a & b) # Output: 1
x = [1, 2, 3]
y = [1, 2, 3]
print(x is y) # Output: False
fruits = ["apple", "banana", "cherry"]
print("apple" in fruits) # Output: True
x = 10 # Integer
y = 2.5 # Float
print(x + y) # Output: 12.5
x = 10.5
y = int(x) # Convert float to int
print(y) # Output: 10