Python is a dynamic and flexible programming language that allows users to work with various data types without explicitly defining them. This article explores number types, variable declaration, dynamic typing, strings, and indexing in Python.
1. Variable Definition in Python
In Python, variables do not require explicit type declaration. Their type and value are automatically assigned based on the input:
x = 5 # Numeric variable name = "Python" # String variable pi = 3.14 # Floating-point number
Key characteristics of variables in Python:
Dynamic: No need for manual type definition
Mutable: Values can be reassigned during execution
2. Dynamic Typing
Python automatically assigns variable types:
x = 5 print(type(x)) # Output: x = "Hello" print(type(x)) # Output:
No need to declare type
Automatic type conversion based on new values
Highly flexible for programming
3. Numeric Types in Python
Python supports several numeric types:
Type | Example | Description |
int | 5 | Integer numbers |
float | 3.14 | Decimal numbers |
complex | 2 + 3j | Complex numbers |
Mathematical operations can be performed on these types:
a = 10 b = 3.5 print(a + b) # Output: 13.5
4. Strings (str) in Python
Strings are sequences of characters, defined using either " " or ' ':
text = "Hello, Python!"
Key features:
Indexable
Mutable (modifiable)
Versatile for text processing
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 words
5. Indexing in Python
Strings are indexed, starting from 0:
word = "Python" print(word[0]) # P print(word[-1]) # n (last character)
String Slicing
Extract specific portions of a string:
message = "Programming" print(message[0:5]) # Output: Progr print(message[:7]) # Extract from start to index 7 print(message[3:]) # Extract from index 3 to end