1. Home
  2. Explore
  3. Computer Science
  4. Python Variables Basics

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Python Variables Basics

Python Variables - Comprehensive Study Notes

What is Python?

Python is one of the most popular programming languages, used by companies like Google and by many universities as an introductory language. Its simple syntax makes it fairly easy to learn. Python is applied in many areas:

  • Web back-end code (code running on servers)

  • Data analysis and scientific research

  • General-purpose programming

Installing Python and Jupyter Notebook

Using Anaconda

Anaconda is a package manager that installs Python, Jupyter Notebook, and other scientific libraries together in one step. To install:

  1. Go to anaconda.org and click Download Anaconda

  2. Choose your platform (Windows, macOS, Linux)

  3. Select Python 3.x (not Python 2)

  4. Download and run the installer, following the prompts

  5. After installation, launch Anaconda Navigator and click Launch under Jupyter Notebook

Jupyter Notebook Overview

Jupyter Notebook has two components:

  • Server: runs in a command-line window; closing it stops the notebook

  • Browser interface: opens at localhost:8888, acting as the user interface

Key characteristics:

  • Code is written in cells — each cell contains one or more lines of code

  • Run a cell using the Run button; output appears below the cell

  • Notebook files have the extension .ipynb

The print() Function

The print() function outputs text or numbers to the screen.

print("Hello world")   # prints: Hello world
print('single quotes') # also valid
print(3)               # prints: 3
  • Strings are enclosed in double quotes "..." or single quotes '...'

  • Multiple print() statements in one cell execute in order

  • Errors occur if quotes, parentheses, or brackets are missing or mismatched

Variables in Python

What is a Variable?

A variable is like a name tag that refers to a value. This differs from the "box" analogy used in some other languages — Python variables do not contain values directly; they refer to them.

a = 1           # variable a refers to the integer 1
b = 2           # b refers to 2
c = "hello"     # c refers to the string "hello"

Key definition: a = 1 means "assign the value 1 to the variable named a" — or more technically, "the name a now refers to the value 1."

To see what a variable refers to, pass it to print() without quotes:

print(a)   # prints: 1
print(c)   # prints: hello

Assignment and Reassignment

Variables can be reassigned to new values, even of a different type:

b = 2
b = 1          # now b refers to 1
b = "ah"       # now b refers to the string "ah"

Multiple variables can refer to the same value:

d = 2          # d refers to 2
b = 2          # b also refers to 2 (same value object)

Attempting to print a variable that has not been defined causes a NameError.

Referencing One Variable from Another

When you assign a variable to another variable, the second variable refers to the value the first variable refers to — not to the variable itself.

a = 1
c = "hello world"
f = a          # f now refers to 1 (the value a refers to)

Later changing a does not change f:

a = 2          # a now refers to 2
print(f)       # still prints: 1

This behavior is important to understand for operations like swapping values.

Swapping Variable Values

Problem Statement

Given two variables:

v1 = "first string"
v2 = "second string"

Swap their values so that v1 becomes "second string" and v2 becomes "first string", without repeating the literal strings.

Naive (Incorrect) Solution

v1 = v2
v2 = v1

After the first line, v1 refers to "second string". Then v2 = v1 makes v2 also refer to "second string". Both variables end up with the same value — the original "first string" is lost.

Correct Solution Using One Temporary Variable

temp = v1      # temp refers to "first string"
v1 = v2        # v1 now refers to "second string"
v2 = temp      # v2 now refers to "first string"

This common programming pattern preserves the original value of v1 in a temporary variable, then uses it to complete the swap.

Alternative: Two Temporary Variables

temp1 = v1
temp2 = v2
v1 = temp2
v2 = temp1

This works but uses an extra variable unnecessarily. The single-temp-variable approach is preferred for efficiency and clarity.

Key Takeaways

  • Variables are name tags that refer to values, not boxes containing values

  • Assignment (=) makes a variable refer to a value; reassignment changes that reference

  • Assigning one variable to another copies the reference, not the variable name

  • Swapping two variables requires a temporary variable to hold one value while reassigning

  • Jupyter Notebook cells allow quick testing of code snippets

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent