Python Data Types
Understanding
data types is fundamental to programming in Python. Data types define the kind
of data that can be stored and manipulated within a program.
Numeric Types
Python
supports three distinct numeric types:
Integers
(int): Whole numbers without a decimal point.
Floating-Point
Numbers (float): Numbers with a decimal point.
Complex
Numbers (complex): Numbers with a real and an imaginary part.
Code:
# Integer
x = 10
#
Floating-Point
y = 3.14
# Complex
z = 1 + 2j
print(type(x)) # <class 'int'>
print(type(y)) # <class 'float'>
print(type(z)) # <class 'complex'>
String
Type
Strings in
Python are sequences of characters enclosed in single quotes ('), double quotes
("), or triple quotes (''' or """).
Code:
# Single
quotes
str1 =
'Hello, World!'
# Double
quotes
str2 =
"Python is fun!"
# Triple
quotes (for multi-line strings)
str3 =
'''This is a
multi-line
string.'''
print(str1)
print(str2)
print(str3)
List Type
A list is an
ordered collection of items, which can be of different types. Lists are
mutable, meaning their elements can be changed.
Code:
my_list = [1,
2, 3, "apple", "banana"]
print(my_list)
# Accessing
elements
print(my_list[0]) # 1
print(my_list[3]) # apple
# Modifying
elements
my_list[1] =
"orange"
print(my_list)
Tuple Type
A tuple is
similar to a list, but it is immutable, meaning once created, its elements
cannot be changed.
Code:
my_tuple =
(1, 2, 3, "apple", "banana")
print(my_tuple)
# Accessing
elements
print(my_tuple[0]) # 1
print(my_tuple[3]) # apple
# Trying to
modify an element (will raise an error)
# my_tuple[1]
= "orange"
Dictionary Type
A dictionary
is an unordered collection of key-value pairs. Each key is unique and
immutable, while the values can be of any type and can be modified.
Code:
my_dict =
{"name": "John", "age": 30, "city":
"New York"}
print(my_dict)
# Accessing
elements
print(my_dict["name"]) # John
print(my_dict["age"]) # 30
# Modifying
elements
my_dict["age"]
= 31
print(my_dict)
Set Type
A set is an
unordered collection of unique elements. Sets are mutable, but their elements
must be immutable.
Code:
my_set = {1,
2, 3, 3, 4, 5}
print(my_set) # {1, 2, 3, 4, 5}
# Adding
elements
my_set.add(6)
print(my_set)
# Removing
elements
my_set.remove(2)
print(my_set)
Control Flow
Control flow
statements allow you to execute specific blocks of code based on certain
conditions or repeatedly execute a block of code.
Conditional Statements
Conditional
statements (if, elif, else) execute different blocks of code based on
conditions.
Code:
x = 10
if x > 0:
print("x is positive")
elif x <
0:
print("x is negative")
else:
print("x
is zero")
Loops
Loops allow
you to execute a block of code repeatedly.
for Loop
The for loop
iterates over a sequence (such as a list, tuple, or string) or other iterable
objects.
Code:
# Iterating
over a list
fruits =
["apple", "banana", "cherry"]
for fruit in
fruits:
print(fruit)
# Iterating
over a range of numbers
for i in
range(5):
print(i)
while Loop
The while
loop executes as long as a condition is true.
Code:
count = 0
while count
< 5:
print(count)
count
+= 1
Break and Continue Statements
The break
statement exits the loop, while the continue statement skips the rest of the
code inside the loop for the current iteration and jumps to the next iteration.
Code:
# Using break
for i in
range(10):
if i == 5:
break
print(i)
# Using
continue
for i in
range(10):
if i % 2 == 0:
continue
print(i)
No comments:
Post a Comment