Python is a versatile programming language that supports various data types, including numbers, strings, and booleans. Understanding these data types and how to work with them is essential for writing effective Python code. This guide will cover Python numbers, strings, booleans, and type casting, along with examples and best practices.
Python Numbers
Python supports three types of numeric data: integers, floating-point numbers, and complex numbers.
Integers
Integers are whole numbers without a decimal point. They can be positive, negative, or zero.
Example:
x = 10
y = -5
z = 0
Floating-Point Numbers
Floating-point numbers, or floats, are numbers with a decimal point. They can represent fractional values.
Example:
pi = 3.14159
negative_float = -0.001
Complex Numbers
Complex numbers consist of a real part and an imaginary part, represented as a + bj
, where a
is the real part and b
is the imaginary part.
Example:
complex_num = 2 + 3j
Python Strings
Strings are sequences of characters enclosed in single quotes ('
) or double quotes ("
). They are used to represent text.
Example:
greeting = "Hello, World!"
name = 'Alice'
String Operations
Strings support various operations, such as concatenation, repetition, and slicing.
Example:
# Concatenation
full_name = "John" + " " + "Doe"
# Repetition
repeated_string = "Hello" * 3
# Slicing
substring = greeting[0:5] # "Hello"
String Methods
Python provides several built-in methods for manipulating strings, such as upper()
, lower()
, strip()
, replace()
, and split()
.
Example:
text = " Hello, World! "
# Convert to uppercase
upper_text = text.upper() # " HELLO, WORLD! "
# Convert to lowercase
lower_text = text.lower() # " hello, world! "
# Remove leading and trailing whitespace
stripped_text = text.strip() # "Hello, World!"
# Replace a substring
replaced_text = text.replace("World", "Python") # " Hello, Python! "
# Split the string into a list
split_text = text.split(",") # [" Hello", " World! "]
Python Booleans
Boolean data types represent one of two values: True
or False
. They are commonly used in conditional statements and logical operations.
Example:
is_active = True
is_logged_in = False
Boolean Operations
Booleans support logical operations such as and
, or
, and not
.
Example:
x = True
y = False
print(x and y) # False
print(x or y) # True
print(not x) # False
Comparison Operators
Booleans are often used with comparison operators to evaluate expressions.
Example:
a = 10
b = 5
print(a > b) # True
print(a == b) # False
print(a != b) # True
When to Use Booleans
Booleans are used in situations where you need to evaluate conditions and make decisions based on those conditions. They are commonly used in if
statements, loops, and logical operations.
Example:
age = 20
# Check if the person is an adult
is_adult = age >= 18
if is_adult:
print("The person is an adult.")
else:
print("The person is not an adult.")
In this example, the boolean variable is_adult
is used to determine whether the person is an adult based on their age.
Type Casting
Type casting, or type conversion, is the process of converting a value from one data type to another. Python provides several built-in functions for type casting, including int()
, float()
, str()
, and bool()
.
Example:
# Convert integer to float
x = 10
y = float(x) # y is now 10.0
# Convert float to integer
pi = 3.14
radius = int(pi) # radius is now 3
# Convert integer to string
age = 30
age_str = str(age) # age_str is now "30"
# Convert string to boolean
is_valid = "True"
is_valid_bool = bool(is_valid) # is_valid_bool is now True
Type Casting Examples
Integer to Float
x = 5
y = float(x)
print(y) # Output: 5.0
Float to Integer
pi = 3.14
radius = int(pi)
print(radius) # Output: 3
String to Integer
num_str = "100"
num_int = int(num_str)
print(num_int) # Output: 100
Integer to String
age = 25
age_str = str(age)
print(age_str) # Output: "25"
Conclusion
Understanding Python numbers, strings, booleans, and type casting is essential for writing effective and efficient code. This guide covered the basics of these data types, along with examples and best practices. By mastering these concepts, you can write more robust and versatile Python programs. Happy coding!