DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk4 min

How Python Variables and Data Types Work: A Beginner’s Guide

Python variables are names bound to objects. Learn how assignment works, what the core data types do, and why changing or copying a list can surprise beginners.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In Python, a variable is a name bound to an object, and the equals sign (=) assigns a value to that name. A type tells Python what kind of object it is—such as a number, text, or collection—and what operations make sense for it. Once you understand assignment and the difference between changeable and unchangeable objects, common beginner code becomes much easier to follow.

What is a variable in Python?

A variable is best understood as a label attached to an object, not a box that permanently contains a value. In count = 3, Python binds the name count to the integer object 3. The equals sign assigns a value to a variable name; it does not mean that the name and object are the same thing. See the Python Tutorial’s introduction.

As an Amazon Associate I earn from qualifying purchases.

count = 3
count = 4

The second assignment binds count to a different integer object. The name can be assigned again, and Python does not require a separate declaration before assignment. But a name must have a value before you use it: trying to read an unassigned name raises NameError.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Assignment is different from comparison

Use one equals sign, =, to assign a value. In a condition, two equals signs, ==, test whether values are equal:

score = 10          # assign 10 to score
print(score == 10)  # test whether score equals 10

What are the basic data types in Python?

A type describes an object’s kind and helps determine what you can do with it. These built-in types cover many beginner tasks:

  • int: whole-number values, such as 7.
  • float: numbers represented with a fractional part, such as 2.5.
  • str: text, written inside quotes, such as 'hello'.
  • list: an ordered, changeable collection, such as [3, 5, 8].
  • tuple: an ordered sequence whose item positions cannot be reassigned, such as (3, 5, 8).
  • set: an unordered collection of unique elements, such as {3, 5, 8}.
  • dict: a collection of key:value pairs, such as {'name': 'Mina'}.

Numbers: int and float

Python distinguishes integer and floating-point values. Ordinary division with / produces a float, even when the result is a whole number. Floor division with // rounds the quotient down to the nearest integer, and % gives the remainder.

print(7 / 2)   # 3.5
print(7 // 2)  # 3
print(7 % 2)   # 1

Text: str

A string is a sequence of characters. You can read a character by its index or take a slice of the text, but you cannot replace one character in the existing string. To change the text, create a new string and assign it to a name.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
greeting = 'hello'
print(greeting[0])    # h
print(greeting[1:4])  # ell
greeting = 'j' + greeting[1:]  # a new string: jello

Collections: sequences, sets, and dictionaries

Collections group multiple values, but they serve different purposes. Lists and tuples are sequences: their elements have positions, so you can access them by index. Sets are for unique membership rather than a stable position or display order. Dictionaries map keys to values, so you look up a value by its key rather than a numeric index.

Choose based on what the data needs to do, not on a universal ranking:

  • Use a list for an ordered sequence you expect to change.
  • Use a tuple when the sequence’s item positions should not be reassigned.
  • Use a set when you need unique elements or want to check membership.
  • Use a dict when you need to retrieve values using meaningful keys.

Lists and tuples can contain repeated values. Sets keep unique elements, so adding a duplicate does not create another distinct member. A dictionary stores values under keys, which must be unique within that dictionary.

What is the difference between a list and a tuple?

Both lists and tuples are ordered sequences, but a list is mutable and a tuple is immutable. Mutable means the object can be changed in place; immutable means it cannot. You can replace a list item or append an item without making a new list. You cannot assign a different value to an individual tuple position.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
colors = ['red', 'green']
colors[1] = 'blue'
colors.append('gold')
print(colors)  # ['red', 'blue', 'gold']

point = (4, 9)
# point[0] = 5  # raises TypeError

A one-item tuple needs a trailing comma. Without the comma, parentheses simply group the value:

single_value = ('hello',)  # tuple
not_a_tuple = ('hello')    # string

Immutability applies to the object itself, not automatically to everything it refers to. A tuple can contain a mutable object such as a list. The tuple’s reference to that list cannot be replaced by item assignment, but the list can still be changed:

group = (['red', 'green'], 'colors')
group[0].append('blue')
print(group)  # (['red', 'green', 'blue'], 'colors')

The Python data model documentation describes mutability as a property of an object’s type; an immutable container can refer to a mutable object.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Does assigning a list copy it?

No. Assignment binds another name to the same list object. If either name is used to mutate that list, the change is visible through both names.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
colors = ['red', 'green']
other_name = colors
other_name.append('blue')
print(colors)  # ['red', 'green', 'blue']

This behavior is called aliasing: multiple names refer to one object. It is useful when shared access is intended, but can be surprising if you expect a separate list.

Make a shallow copy when you need a separate outer list

A slice such as colors[:] creates a shallow copy of the outer list. Adding or replacing an item in one outer list will not change the other. However, if the list contains nested mutable objects, those inner objects are still shared.

original = ['red', 'green']
copy = original[:]
copy.append('blue')
print(original)  # ['red', 'green']
print(copy)      # ['red', 'green', 'blue']

How to choose a type for a beginner program

Start with the operations you need. If you are storing one value, use a number or string. If you need several values in order, use a list when they may change and a tuple when their positions should stay fixed. Choose a set when duplicates should collapse into one membership, and a dictionary when each value should be found by a key.

For a fuller reference, the official Python Tutorial says it is aimed at programmers new to Python rather than people entirely new to programming. It covers more than this introduction, so beginners without programming experience may need to take its examples slowly and look up unfamiliar terms.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.