Skip to main content

Command Palette

Search for a command to run...

🐍 Python – Part 1: Understanding Variables and Data Types

Published
4 min readView as Markdown

If you're new to Python, one of the first things you'll learn is how to work with variables and data types. Python makes it super easy and beginner-friendly. This post will walk you through the basics with clear examples, rules, naming conventions, and bonus tips.


✅ What Is a Variable?

A variable in Python is like a box that stores a value. You give it a name and use it later in your code.

pythonCopyEditname = "Lorenzo"
age = 25
height = 5.9
is_happy = True

Here’s what’s happening:

  • name is a str (string)

  • age is an int (integer)

  • height is a float

  • is_happy is a bool (Boolean)


✅ Quick Rules for Variables

  • Strings go in quotes: "text"

  • No need to declare variable types like in Java or C++

  • Variable names can’t start with a number

  • Python is case-sensitive: age and Age are two different variables


🧠 Python Variable Naming Conventions

RuleExample
Must start with a letter or an underscorename, _value
Cannot start with a number2value, 3name
Can only contain letters, numbers, _user_name1, age2
Are case-sensitiveNamename
Avoid using Python keywords as variable namesclass, if, while

  • Use snake_case for variable names: user_name, total_amount

  • Keep names descriptive but short: age, user_id

  • Constants should be ALL_CAPS: PI = 3.14, MAX_USERS = 100

🧠 What are constants?
Python doesn’t have real constants, but by convention, if you name a variable in ALL CAPS, it means "don’t change this later in the program."

pythonCopyEditPI = 3.14
MAX_USERS = 100

📘 Common Python Variable Types

TypeExampleDescription
int5Whole numbers
float5.8Numbers with decimals
str"hello"Text (strings)
boolTrue, FalseBoolean values
list[1, 2, 3]Ordered, changeable collection
tuple(1, 2, 3)Ordered, unchangeable collection
set{1, 2, 3}Unordered, no duplicates
dict{"name": "Lorenzo"}Key-value pairs
NoneTypeNoneRepresents "nothing" or "no value"
complex2 + 3jComplex numbers (used in advanced math)
bytesb'hello'Immutable sequence of bytes
bytearraybytearray(b'hello')Mutable sequence of bytes
rangerange(5)Represents a sequence of numbers

✅ Built-in Python Data Types (Official Categories)

CategoryData Types Included
Text Typestr
Numeric Typesint, float, complex
Sequence Typeslist, tuple, range
Set Typesset, frozenset
Mapping Typedict
Boolean Typebool
Binary Typesbytes, bytearray, memoryview
None TypeNoneType (only value is None)

🧠 Fun Fact

You can check the type of any variable using the type() function:

pythonCopyEditx = 5.8
print(type(x))  # Output: <class 'float'>

This is really useful when you’re debugging or just curious about what kind of data you're working with.


🔁 Dynamic vs Static Typing

Python is a dynamically-typed language.

✅ You don’t need to declare the type of a variable.
🧠 Python figures it out when the program runs (at runtime).

🔍 Example:

pythonCopyEditx = 5       # int
x = "text"  # Now it's a string

This is allowed in Python, unlike in Java or C++.


📊 Comparison Table

LanguageType SystemExample
PythonDynamically typedx = 10
JavaStatically typedint x = 10;
C++Statically typedfloat x = 10.5;

More from this blog

Data Science

39 posts