Intro to Python

Overview

Welcome to the Introduction to Python for Data Science module! Python is the most widely used programming language in data science, valued for its simplicity and powerful libraries. In this first lesson, you'll learn the building blocks of Python: how to store data in variables and how Python classifies different kinds of data using types.

Learning Objective

By the end of this lesson, you will have learned how to:

  • Declare and use variables in Python.
  • Understand Python data types (int, float, bool, str).

Key terms

Variable: A named container that stores a value in memory, allowing you to reference and reuse it throughout your program.

Data type: A classification that tells Python what kind of value a variable holds and what operations can be performed on it.

Integer (int): A whole number without a decimal point, such as 5, -3, or 100.

Float (float): A number with a decimal point, such as 3.14, -0.5, or 2.0.

Boolean (bool): A data type with only two possible values: True or False.

String (str): A sequence of characters enclosed in quotes, such as "hello" or "data science".

Starter Code

This lesson encourages experimentation. Use the included Colaboratory notebook to run the code as you learn, or read the lesson and then freely experiment with the notebook's code.

Introduction

Python is a beginner-friendly programming language that reads almost like plain English, making it an excellent first language for aspiring data scientists. Before you can analyze data or build models, you need to understand how Python stores and works with information. That starts with two fundamental concepts: variables and data types.

Think of a variable as a labeled box — you can put a value inside, give the box a name, and then refer to that name whenever you need the value. Data types tell Python (and you!) what kind of thing is inside the box, so Python knows what you can do with it.

Variables

Declaring a Variable

In Python, you create a variable by writing its name, followed by an = sign, and then the value you want to store. No special keyword is needed — Python figures out the type automatically.

# Declaring variables
name = "Alex"
age = 25
height = 5.9
is_student = True

Here, we've created four variables:

  • name stores the text "Alex"
  • age stores the whole number 25
  • height stores the decimal number 5.9
  • is_student stores True

Using Variables

Once declared, you can use a variable anywhere in your code by referring to its name:

print(name)        # Output: Alex
print(age)         # Output: 25
print(height)      # Output: 5.9
print(is_student)  # Output: True

You can also update a variable's value by simply reassigning it:

age = 26
print(age)  # Output: 26

Naming Rules

Python variable names must follow a few rules:

  • Names can only contain letters, numbers, and underscores (_)
  • Names cannot start with a number
  • Names are case-sensitive (name and Name are different variables)
  • Use descriptive names to make your code readable (e.g., student_count instead of sc)

Data Types

Integers (int)

Integers are whole numbers — positive, negative, or zero. They are commonly used for counting and indexing.

num_students = 30
temperature = -5
year = 2024

print(type(num_students))  # Output: <class 'int'>

The built-in type() function tells you the data type of any variable.

Floats (float)

Floats are numbers that include a decimal point. They are used when precision matters, such as measurements or calculations.

pi = 3.14159
average_score = 87.5
price = 19.99

print(type(pi))  # Output: <class 'float'>

Booleans (bool)

Booleans can only be True or False. They are often the result of comparisons and are essential for controlling program flow.

is_raining = False
passed_exam = True

print(type(is_raining))   # Output: <class 'bool'>
print(10 > 5)             # Output: True
print(3 == 7)             # Output: False

Strings (str)

Strings are sequences of characters enclosed in either single (') or double (") quotes. They are used to represent text.

greeting = "Hello, World!"
course = 'Data Science'

print(type(greeting))  # Output: <class 'str'>
print(len(greeting))   # Output: 13

The len() function returns the number of characters in a string.

Basic Operations

Arithmetic with Numbers

Python supports standard arithmetic operations on integers and floats:

a = 10
b = 3

print(a + b)   # Addition:       13
print(a - b)   # Subtraction:    7
print(a * b)   # Multiplication: 30
print(a / b)   # Division:       3.3333...
print(a // b)  # Floor division: 3
print(a % b)   # Modulus:        1
print(a ** b)  # Exponentiation: 1000

Combining Types

You can mix integers and floats in calculations — Python automatically produces a float:

result = 5 + 2.0
print(result)       # Output: 7.0
print(type(result)) # Output: <class 'float'>

Type Conversion

You can convert between types using built-in functions:

x = "42"
x_as_int = int(x)       # Convert string to int
y = 3
y_as_float = float(y)   # Convert int to float
z = 100
z_as_str = str(z)       # Convert int to string

print(type(x_as_int))   # Output: <class 'int'>

Conclusion

In this lesson, you learned the foundational concepts of Python programming: how to declare and use variables, and how Python's four basic data types — int, float, bool, and str — classify different kinds of data. These building blocks appear in every Python program you will write. In the next lesson, you will learn how to organize and reuse your code by writing functions.

Practice

Coding Assessment

Practice the concepts from this lesson using this Colaboratory notebook. After completing the exercises, save your notebook to GitHub and submit the link for grading.

Knowledge Check

Question 1: Which of the following is a valid Python variable name?

Question 2: What data type does Python assign to the value `3.14`?

Question 3: What does the `type()` function do in Python?