Variables and Data Types in Python
Contents
Variables in Python
- Variables are used to store data that can be referenced and manipulated in the program.
- A variable is given to a memory location where data is stored.
- Python is a dynamically-typed language, which means that you don’t need to declare the type of a variable explicitly. Python determines the type based on the value assigned to the variable.
Example 1: Check variable type
name = "Alice"
age = 25
is_valid = True
print(type(name))
print(type(age))
print(type(is_valid))
Code language: PHP (php)
- Output:
<class 'str'>
<class 'int'>
<class 'bool'>
Code language: HTML, XML (xml)
Example 2: Check the memory address of a variable
x = 100
print(id(x))
Code language: PHP (php)
- Output:
140734924203544
Rules for Naming Variables
- A variable name must start with a letter (a-z, A-Z) or an underscore (_).
- It can contain letters, digits (0-9), and underscores, but cannot start with a digit.
- Variable names are case-sensitive (
age
andAge
are different).
Data Types in Python
We will discuss the main categories of Data types in Python:
I- Numeric Types
- Integer (int): numbers, positive or negative.
Example:
age = 30
print(age)
print(type(age))
Code language: PHP (php)
Output:
30
<class 'int'>
Code language: HTML, XML (xml)
- Float: Numbers with decimals
Example:
price = 19.99
print(price)
print(type(price))
Code language: PHP (php)
Output:
19.99
<class 'float'>
Code language: HTML, XML (xml)
- Complex: Numbers: Number with real and imaginary numbers
Example:
z = 2 + 3j
print(z)
print(type(z))
Code language: PHP (php)
Output:
(2+3j)
<class 'complex'>
Code language: HTML, XML (xml)
II- String
str: Sequence of characters enclosed in quotes:
Example:
LD= "Hello! This DearnDuty.com"
print(LD)
print(type(LD))
Code language: PHP (php)
Output:
Hello! This DearnDuty.com
<class 'str'>
Code language: HTML, XML (xml)
III- Boolean Type
It include a True or False value
Example:
is_active = True
print(is_active)
print(type(is_active))
Code language: PHP (php)
Output:
True
<class 'bool'>
Code language: PHP (php)
IV- Sequence Types
- List: a list is an ordered, mutable (modifiable) collection of items.
Example:
fruits = ["apple", "banana", "cherry"]
print(fruits)
print(type(fruits))
Code language: PHP (php)
Output:
['apple', 'banana', 'cherry']
<class 'list'>
Code language: JavaScript (javascript)
- Tuple: Ordered, immutable collection of items
Example:
coordinates = (10, 20)
print(coordinates)
print(type(coordinates))
Code language: PHP (php)
Output:
(10, 20)
<class 'tuple'>
Code language: HTML, XML (xml)
- Set: Unordered collection of unique items.
Example:
unique_numbers = {1, 2, 3, 4, 5}
print(unique_numbers)
print(type(unique_numbers))
Code language: PHP (php)
Output:
{1, 2, 3, 4, 5}
<class 'set'>
Code language: HTML, XML (xml)
V- Dictionary
Unordered collection of key-value pairs.
Example:
person = {"name": "Bil", "age": 30}
print(unique_numbers)
print(type(unique_numbers))
Code language: PHP (php)
Output:
{1, 2, 3, 4, 5}
<class 'set'>
Code language: HTML, XML (xml)
VI- None
Indicate the absence of a value.
value = None
Examples of Variable Assignments and Operations
Single and multiple assignement
- Single assignement:
x = 5
- Multiple assignement:
Example:
# Multiple assignments
a, b, c = 10, 20, 30
# Assign the same value to multiple variables
m = n = 0
print(a, b , c)
print(m)
print(n)
Code language: PHP (php)
Output:
10 20 30
0
0
Using Variables in Expressions python
num1 = 10
num2 = 20
result = num1 + num2
print(result)
Code language: PHP (php)
output
30
Convert Data type
Example:
y = float(result)
print(y)
print(type(y))
Code language: PHP (php)
Output:
30.0
<class 'float'>
Code language: HTML, XML (xml)
Check the