Python Variables & Data Types for AI/ML

Master Python variables & data types. Essential for AI, ML, and data science beginners. Learn dynamic typing & core concepts for efficient coding.

Python Variables & Data Types

This document provides a comprehensive overview of variables and fundamental data types in Python, designed to be clear, readable, and technically accurate.


1.1 Python Variables

In Python, a variable is a symbolic name that represents a value stored in memory. Unlike some other programming languages, Python does not require you to declare the type of a variable before assigning a value to it. Python is dynamically typed, meaning the type of a variable is inferred at runtime based on the value assigned.

Creating Variables

You create a variable by assigning a value to it using the assignment operator (=).

Example:

# Assigning an integer value
age = 30

# Assigning a string value
name = "Alice"

# Assigning a floating-point value
height = 5.9

Variable Naming Rules

  • Start with a letter or underscore: Variable names must begin with a letter (a-z, A-Z) or an underscore (_).
  • Contain only alphanumeric characters and underscores: After the first character, variable names can contain letters, numbers (0-9), and underscores.
  • Case-sensitive: age, Age, and AGE are considered three different variables.
  • Avoid Python keywords: Do not use reserved Python keywords (like if, for, while, class, def, etc.) as variable names.

Good Examples: my_variable, user_name, count_1, _private_variable

Bad Examples: 1variable (starts with a number), my-variable (contains a hyphen), for (a keyword)


1.2 Python Data Types

Python has a rich set of built-in data types that categorize the kind of data a variable can hold. Understanding these types is crucial for effective programming.


1.3 Python Numbers

Python supports various numeric types to represent numbers:

  • Integer (int): Whole numbers, positive or negative, without decimals.
    quantity = 100
    negative_number = -50
  • Floating-point Number (float): Numbers, positive or negative, containing one or more decimals.
    price = 19.99
    pi_value = 3.14159
  • Complex Number (complex): Numbers with a real and imaginary part, represented as x + yj.
    complex_num = 2 + 3j

Checking Data Types: You can use the type() function to determine the data type of a variable.

Example:

x = 10
y = 2.5
z = 1 + 2j

print(type(x))  # Output: <class 'int'>
print(type(y))  # Output: <class 'float'>
print(type(z))  # Output: <class 'complex'>

1.4 Type Casting in Python

Type casting (also known as type conversion) is the process of converting a variable from one data type to another. Python provides built-in functions for this purpose.

  • int(): Converts a value to an integer.
  • float(): Converts a value to a floating-point number.
  • str(): Converts a value to a string.
  • complex(): Converts a value to a complex number.

Example:

# Converting float to int
num_float = 9.8
num_int = int(num_float)
print(num_int)  # Output: 9 (decimal part is truncated)

# Converting int to float
num_int_to_float = 5
num_float_converted = float(num_int_to_float)
print(num_float_converted)  # Output: 5.0

# Converting number to string
number_to_string = 123
string_version = str(number_to_string)
print(string_version)       # Output: '123'
print(type(string_version)) # Output: <class 'str'>

# Converting string to int (if the string represents a valid integer)
string_number = "456"
int_from_string = int(string_number)
print(int_from_string)      # Output: 456

# Attempting to convert a non-numeric string to a number will raise a ValueError
# invalid_string = "hello"
# int(invalid_string) # This would cause a ValueError

1.5 Python Strings

A string is a sequence of characters, used to represent text. Strings in Python are immutable, meaning once created, their contents cannot be changed. They are enclosed in either single quotes (' ') or double quotes (" ").

Creating Strings

# Using single quotes
message_single = 'Hello, Python!'

# Using double quotes
message_double = "Welcome to the world of data types."

# Using triple quotes for multi-line strings
multi_line_string = """This is a string
that spans multiple
lines."""

Accessing String Characters

You can access individual characters in a string using indexing, starting from 0 for the first character. Negative indexing can be used to access characters from the end of the string.

Example:

text = "Python"

# Accessing the first character
first_char = text[0]  # 'P'

# Accessing the third character
third_char = text[2]  # 't'

# Accessing the last character using negative indexing
last_char = text[-1]  # 'n'

String Slicing

String slicing allows you to extract a portion (substring) of a string. It uses the syntax [start:stop:step].

  • start: The index of the first character to include (inclusive).
  • stop: The index of the character to stop at (exclusive).
  • step: The interval between characters.

Example:

sentence = "Programming is fun!"

# Get characters from index 0 up to (but not including) index 11
substring1 = sentence[0:11]  # "Programming"

# Get characters from index 12 to the end
substring2 = sentence[12:]   # "is fun!"

# Get characters from the beginning up to (but not including) index 11
substring3 = sentence[:11]   # "Programming"

# Get every second character
every_second = sentence[::2] # "Pormig sfn"

# Reverse the string
reversed_string = sentence[::-1] # "!nuf si gnimmargorP"

1.6 Python String Methods

Python strings come with a variety of built-in methods that perform common operations. These methods return new strings; they do not modify the original string due to immutability.

Here are some commonly used string methods:

  • upper(): Converts all characters to uppercase.
    my_string = "hello world"
    print(my_string.upper())  # Output: HELLO WORLD
  • lower(): Converts all characters to lowercase.
    my_string = "HELLO WORLD"
    print(my_string.lower())  # Output: hello world
  • strip(): Removes leading and trailing whitespace.
    padded_string = "   whitespace   "
    print(padded_string.strip())  # Output: whitespace
  • replace(old, new): Replaces all occurrences of a substring with another substring.
    old_string = "I like cats. Cats are cute."
    new_string = old_string.replace("cats", "dogs")
    print(new_string)  # Output: I like dogs. Dogs are cute.
  • split(separator): Splits the string into a list of substrings based on a given separator. If no separator is specified, it splits by whitespace.
    csv_data = "apple,banana,cherry"
    fruit_list = csv_data.split(',')
    print(fruit_list)  # Output: ['apple', 'banana', 'cherry']
    
    sentence_to_words = "This is a sentence."
    word_list = sentence_to_words.split()
    print(word_list)  # Output: ['This', 'is', 'a', 'sentence.']
  • join(iterable): Joins elements of an iterable (like a list) into a single string, using the string as a separator.
    my_list = ["red", "green", "blue"]
    joined_string = "-".join(my_list)
    print(joined_string)  # Output: red-green-blue
  • find(substring): Returns the lowest index in the string where the substring is found. Returns -1 if not found.
    text = "programming is fun"
    index = text.find("is")
    print(index)  # Output: 12
  • startswith(prefix): Returns True if the string starts with the specified prefix, otherwise False.
    file_name = "report.txt"
    print(file_name.startswith("rep"))  # Output: True
  • endswith(suffix): Returns True if the string ends with the specified suffix, otherwise False.
    file_name = "report.txt"
    print(file_name.endswith(".txt"))  # Output: True

1.7 Python Boolean

The Boolean data type (bool) represents one of two values: True or False. Booleans are fundamental for conditional logic and control flow in programming.

Creating Boolean Values

Boolean values are typically the result of comparison operations.

Example:

# Direct assignment
is_active = True
is_logged_in = False

# Results of comparison operations
x = 10
y = 5

are_equal = (x == y)       # False
is_greater = (x > y)       # True
is_less_equal = (x <= y)   # False

print(type(is_active))     # Output: <class 'bool'>
print(are_equal)           # Output: False
print(is_greater)          # Output: True

Boolean Context

In Python, many values are considered "truthy" or "falsy" when evaluated in a Boolean context (e.g., in an if statement).

Falsy values include:

  • None
  • False
  • Zero of any numeric type (e.g., 0, 0.0, 0j)
  • Empty sequences (e.g., '', (), [])
  • Empty mappings (e.g., {})

Truthy values include:

  • All other values not considered falsy.

Example:

# Truthy examples
if "hello":
    print("This string is truthy.")

if 123:
    print("This number is truthy.")

if [1, 2, 3]:
    print("This list is truthy.")

# Falsy examples
if "":
    print("This string is falsy.") # This will not be printed
else:
    print("This empty string is falsy.")

if 0:
    print("Zero is falsy.") # This will not be printed
else:
    print("Zero is indeed falsy.")