- Overview of Python's Five Fundamental Data Types
Numeric Types
Purpose: Used to represent numerical values such as age, identification numbers, and quantitative data.
Definition: Can be defined directly by assigning a numeric value, or explicitly using the int(), float(), or complex() constructors.
Usage: Supports various operators including % (modulo), // (floor division), ** (exponentiation). For complex numbers, import the cmath module.
String Type
Purpose: Used to represent text data such as names, gender, and other character-based information.
Definition: Defined using single quotes, double quotes, or triple quotes (for multi-line strings). Can also be created using str().
Usage: Supports concatenation with + and repetition with *.
List
Purpose: Used to store multiple elements of any data type in an ordered sequence.
Definition: Created using square brackets [] with comma-separated elements.
Usage: Elements are accessed via zero-based indexing. Supports slicing and various list operations.
Dictionary
Purpose: Used to store key-value pairs where each value has an associated descriptive key.
Definition: Created using curly braces {} with key-value pairs separated by commas. Keys must be strings or hashable types.
Usage: Values are accessed by their key rather than numerical index using dict[key] syntax.
Boolean Type
Purpose: Used to represent conditional logic and decision-making results.
Definition: Rarely defined directly; typically obtained through logical operations or converted using bool().
Usage: All data types have a inherent boolean value: 0, None, empty sequences ("", [], {}), and False evaluate to False; all other values evaluate to True.
- One-Line Variable Assignment
Assign the same value to multiple variables in a single statement:
# Instead of:
x = 10
y = 10
z = 10
# Use:
x = y = z = 10
- Swapping Two Variable Values
Two methods to exchange values between variables:
x = 10
y = 20
# Method 1: Tuple unpacking
x, y = y, x
# Method 2: Using a temporary variable
z = x
x = y
y = z
- Extracting List Elements
Retrieve specific elements from a nested list:
nick_info_dict = {
'name': 'nick',
'age': '18',
'height': 180,
'weight': 140,
'hobby_list': ['read', 'run', 'music', 'code'],
}
# Get the second and third hobbies (indices 1 and 2)
# Using negative indexing: -2 gives second-to-last, -1 gives last
print(nick_info_dict['hobby_list'][1:3])
# Output: ['run', 'music']
- Three String Formatting Methods
Different approaches to format and output variable values:
name = 'Alex'
height = 175
weight = 150
# Method 1: f-string (formatted string literals)
print(f"My name is {name}, my height is {height}, my weight is {weight}")
# Method 2: Percent-style formatting
print("My name is %s, my height is %s, my weight is %s" % (name, height, weight))
# Method 3: str.format() method
print("My name is {}, my height is {}, my weight is {}".format(name, height, weight))
All three methods produce the same output:
My name is Alex, my height is 175, my weight is 150
python,data-types,programming-basics,string-formatting,variable-assignment