Python provides several built-in data types that allow you to store, manipulate, and organize data efficiently. Below is a quick overview of the most commonly used ones.
Core Python Data Types
Integers (
int)
Whole numbers without a decimal point, such as3,300, or200.Floating Point Numbers (
float)
Numbers with decimals, such as2.3,4.6, or100.0.Strings (
str)
Ordered sequences of characters, such as"hello",'Sammy',"2000", or even Unicode strings like"楽しい".Lists (
list)
Ordered collections that can store multiple object types:[10, "hello", 200.3]Dictionaries (
dict)
Unordered collections of key-value pairs:{"mykey": "value", "name": "Frankie"}Tuples (
tuple)
Ordered collections that are immutable (cannot be changed):(10, "hello", 200.3)Sets (
set)
Unordered collections of unique elements:{"a", "b"}Booleans (
bool)
Logical values that representTrueorFalse.
Working with Numbers
Python mainly works with two types of numbers:
Integers (whole numbers)
Floating point numbers (decimals)
You can perform basic mathematical operations and store numeric values using variables, which makes your code easier to read and reuse.
Variable Assignment in Python
Variables allow you to give meaningful names to data so it can be reused later in your program.
Example:
my_dogs = 2
Rules for Naming Variables
Variable names cannot start with a number
No spaces are allowed—use underscores (
_) insteadAvoid special characters like
@,!,$, etc.Use lowercase letters (PEP8 best practice)
Do not use reserved Python keywords like
listorstr
Dynamic Typing in Python
Python uses dynamic typing, meaning you can reassign variables to different data types.
Example:
my_dogs = 2
my_dogs = ["Sammy", "Frankie"]
Pros of Dynamic Typing
Easy and flexible to work with
Faster development time
Cons of Dynamic Typing
Can lead to unexpected bugs
Requires awareness of data types using
type()
This flexibility is different from statically typed languages, where changing a variable’s type would cause an error.
Strings in Python
Strings are ordered sequences of characters created using single or double quotes.
'hello'
"Hello"
"I don't do that"
String Indexing and Slicing
Because strings are ordered, you can:
Index to retrieve a single character
Slice to retrieve a range of characters
Slicing syntax:
[start:stop:step]
start: where the slice beginsstop: where it ends (not included)step: how many characters to jumpString Formatting for Printing
Often, you’ll want to insert variables into strings—this is called string interpolation.
Example:
my_name = "Jose"
print("Hello " + my_name)
Python offers cleaner methods:
.format()methodf-strings (formatted string literals)
Lists
Lists are ordered, mutable collections that can hold different data types.
[1, 2, 3, 4, 5]
Dictionaries
Dictionaries store data using key-value pairs rather than positions.
Example:
{'key1': 'value1', 'key2': 'value2'}
When to Use Lists vs Dictionaries
Lists: Ordered data accessed by position
Dictionaries: Unordered data accessed by key name
Tuples
Tuples are similar to lists but are immutable, meaning their contents cannot be changed once created.
Example:
(1, 2, 3)
They are useful when you want to ensure data remains constant.
Sets
Sets are unordered collections of unique elements. Duplicate values are automatically removed.
They are useful for membership testing and eliminating duplicates.
Booleans
Booleans represent True or False values and are essential for:
Logic
Comparisons
Control flow (if statements, loops)
No comments:
Post a Comment