1. Variable Definition in Python
Python does not require manual type declaration for variables. The type is automatically inferred from the assigned value.
x = 5 # Integer variable name = "Python" # String variable pi = 3.14 # Floating-point variable
- Dynamic: Types are inferred automatically
- Mutable: Variable values can be reassigned during execution
2. Dynamic Typing
Python supports dynamic typing—variable types change based on the value assigned:
x = 5 print(type(x)) # Output:x = "Hello" print(type(x)) # Output:
- Flexible for rapid development
- No need for manual type conversion
3. Numeric Types in Python
| Type | Example | Description |
|---|---|---|
| int | 5 | Whole number |
| float | 3.14 | Decimal number |
| complex | 2 + 3j | Complex number (real + imaginary) |
Mathematical operations can be performed directly:
a = 10 b = 3.5 print(a + b) # Output: 13.5
4. Strings in Python
Strings are sequences of characters and can be defined using single or double quotes:
text = "Hello, Python!"
- Indexable and sliceable
- Useful for text processing and transformation
Common string operations:
message = "Python Programming"
print(len(message)) # Count characters
print(message.upper()) # Convert to uppercase
print(message.lower()) # Convert to lowercase
print(message.replace("Python", "Java")) # Replace word
5. Indexing and Slicing
Strings use zero-based indexing:
word = "Python" print(word[0]) # Output: P print(word[-1]) # Output: n (last character)
Slicing extracts specific portions of a string:
message = "Programming" print(message[0:5]) # Output: Progr print(message[:7]) # Output: Program print(message[3:]) # Output: gramming