Numbers and Variables in Python: Definitions

Variables and data types play .....

Numbers Variables Python

~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


TypeExampleDescription
int5Whole number
float3.14Decimal number
complex2 + 3jComplex 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

Related Articles

Complete Guide to Python Decorators – Enhancing Function Behavior with Reusable Logic

Decorators in Python are a powerful tool for modifying or extending the behavior of functions and classes without changing their original code. They allow developers to implement reusable logic such as logging, authentication, caching, or timing in a clean and maintainable way. This article explores the structure, definition, common use cases, and practical examples of decorators in Python.

Continue

Comprehensive Guide to Python Classes – Object-Oriented Design, Structure, Inheritance, and Practical Use

Classes in Python are the foundation of object-oriented programming, allowing developers to define complex data structures, encapsulate logic, and create scalable, maintainable systems. This article walks through the fundamentals of defining classes, constructors, methods, inheritance, encapsulation, and real-world applications in Python projects.

Continue

Several example codes

understand the Python programming language

Continue

Complete Guide to Getting Text Input from Users in Python

In Python, accepting input from users is one of the simplest yet most powerful features for interactive programming. This article explores the input() function in depth, explains how to cast types, validate user data, and use input within loops and conditions. Real-life examples help clarify each concept, making this guide perfect for beginners and intermediate learners.

Continue

Comprehensive Guide to Functions in Python: Structure, Types, and Use Cases

Functions in Python are essential building blocks for writing modular, reusable, and maintainable code. This article introduces function definitions, distinguishes between built-in and user-defined functions, explores parameters and return values, and presents advanced topics such as recursion and lambda expressions. Whether you're a beginner or brushing up for interviews, this guide offers a clear and accessible foundation.

Continue

Loops, Iterators, and Repetition Control in Python

Loops, Iterators, and Repetition Control in Python

Continue