Skip to Content
Course content

2.1.1 Numbers, strings, booleans

Python supports various fundamental data types, but the three most commonly used ones are numbers, strings, and booleans. Understanding these types is essential for working with Python effectively.

1. Numbers in Python

Numbers are used to represent numerical data. Python has three primary numeric types: integers, floating-point numbers, and complex numbers.

a. Integers (int)

  • Integers are whole numbers without a decimal point. They can be positive or negative.
    Example:
    x = 10          # Integer
    y = -5          # Negative integer
    
  • You can perform arithmetic operations such as addition, subtraction, multiplication, and division on integers.
    Example:
    sum = 10 + 5    # 15
    product = 10 * 5  # 50
    

b. Floating-Point Numbers (float)

  • Floats are numbers that have a decimal point.
    Example:
    price = 19.99   # Float
    pi = 3.14159    # Float
    
  • You can also perform arithmetic operations with floats.
    Example:
    area = 10.5 * 2.0  # 21.0
    

c. Complex Numbers (complex)

  • Complex numbers consist of a real and an imaginary part, represented as a + bj, where a is the real part and b is the imaginary part.
    Example:
    z = 3 + 5j  # Complex number (3 is the real part, 5j is the imaginary part)
    
  • Operations like addition, subtraction, and multiplication can also be performed on complex numbers.
    Example:
    c1 = 3 + 5j
    c2 = 1 + 2j
    result = c1 + c2  # (4 + 7j)
    

2. Strings in Python

Strings are sequences of characters used to store text in Python. Strings can be enclosed in either single quotes (') or double quotes ("), and they are immutable, meaning once created, they cannot be changed.

a. Creating Strings

You can create strings in Python using either single or double quotes.

name = "Alice"        # String using double quotes
greeting = 'Hello'    # String using single quotes

b. String Operations

Python provides various operations you can perform on strings, such as concatenation, repetition, and slicing.

  • Concatenation: You can join two or more strings together using the + operator.
    Example:
    full_name = "Alice" + " " + "Smith"  # 'Alice Smith'
    
  • Repetition: You can repeat a string multiple times using the * operator.
    Example:
    repeated = "Hi! " * 3  # 'Hi! Hi! Hi! '
    
  • Slicing: You can extract parts of a string using slicing. The syntax is string[start:end].
    Example:
    name = "Python"
    sub_string = name[0:3]  # 'Pyt'
    

c. String Methods

Strings come with a variety of built-in methods that allow for operations like changing case, trimming whitespace, checking for substrings, etc.

Examples:

sentence = "hello, world!"
print(sentence.upper())  # 'HELLO, WORLD!'
print(sentence.lower())  # 'hello, world!'
print(sentence.strip())  # Remove leading/trailing whitespace

3. Booleans in Python

Booleans are used to represent truth values. They can only have one of two possible values: True or False. Booleans are essential for control flow, such as in conditionals (if statements).

a. Boolean Values

  • True represents a logical truth.
  • False represents a logical falsehood.

Example:

is_active = True   # Boolean value
is_valid = False   # Boolean value

b. Boolean Operations

You can combine or evaluate booleans using logical operators:

  • AND (and): Returns True if both operands are True.
  • OR (or): Returns True if at least one operand is True.
  • NOT (not): Reverses the truth value.

Example:

x = True
y = False

print(x and y)   # False
print(x or y)    # True
print(not x)     # False

c. Boolean Evaluation in Python

In Python, non-zero numbers and non-empty objects are treated as True, and zero and empty objects (like empty strings, lists, and tuples) are treated as False.

Example:

print(bool(1))        # True (1 is a non-zero number)
print(bool(0))        # False (0 is considered False)
print(bool(""))       # False (empty string is considered False)
print(bool([1, 2]))   # True (non-empty list is considered True)

Summary of Numbers, Strings, and Booleans

  • Numbers: Python supports integers, floats, and complex numbers for handling numerical data.
  • Strings: Strings store text and support various operations like concatenation, slicing, and built-in methods.
  • Booleans: Booleans are used to represent True or False values and are crucial for conditional statements and logical operations.

Understanding how to work with these basic data types is essential for building more complex Python programs, as they form the foundation of virtually every program written in Python.

Commenting is not enabled on this course.