~2 min read • Updated Jul 20, 2025
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
Written & researched by Dr. Shahin Siami
SubArticles
Strings in Python
Structure, Properties, and Common Methods Strings are one of the most fundamental data types in Python, used to store and manipulate text.
Tuples in Python
Tuples in Python: Structure, Properties, and Use Cases Tuples are an essential built-in data type in Python, used to store fixed collections of items in a single immutable object.
Lists in Python
Structure, Flexibility, and Practical Usage Lists are one of Python’s most versatile and powerful data structures, providing dynamic storage and manipulation of ordered collections.
Dictionaries in Python
Dictionaries in PythonDictionaries in Python: Structure, Properties, and Efficient Key-Based Access Dictionaries (dict) are one of Python’s most powerful and flexible data structures, enabling fast lookup and management of data via unique keys.
Dictionary vs. List vs. Tuple in Python
A Comparative Guide to Structure and Usage Python offers a range of data types for organizing values efficiently. The most commonly used are lists, tuples, and dictionaries. Each has distinct characteristics that determine its suitability for specific tasks