You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Variables are the building blocks of every Python program. A variable is a name that refers to a value stored in your computer's memory. In this lesson, you will learn how to create variables, understand Python's core data types, and convert between them.
In Python, you create a variable simply by assigning a value to a name using the = operator:
age = 25
name = "Alice"
pi = 3.14159
is_student = True
print(age) # 25
print(name) # Alice
print(pi) # 3.14159
print(is_student) # True
There is no need to declare the type — Python figures it out automatically. This is called dynamic typing.
_if, for, class, return)age and Age are different)# Valid names
user_name = "Alice"
_count = 10
total2 = 50
MAX_SIZE = 100
# Invalid names
2nd_place = "Bob" # starts with a number
my-name = "Charlie" # hyphens not allowed
class = "Math" # 'class' is a reserved keyword
Python developers follow PEP 8 style guidelines:
# Variables and functions: snake_case
user_age = 25
first_name = "Alice"
# Constants: UPPER_SNAKE_CASE
MAX_RETRIES = 3
PI = 3.14159
# Classes: PascalCase (you'll learn about these later)
# class MyClass:
Python has several built-in data types. The four most fundamental are:
int)Whole numbers — positive, negative, or zero:
age = 25
temperature = -10
count = 0
big_number = 1_000_000 # underscores for readability
print(type(age)) # <class 'int'>
print(type(big_number)) # <class 'int'>
Python integers have unlimited precision — they can be as large as your memory allows:
huge = 10 ** 100 # 10 to the power of 100
print(huge) # a 1 followed by 100 zeros
float)Numbers with decimal points:
price = 19.99
pi = 3.14159
negative = -0.5
scientific = 2.5e6 # 2.5 × 10^6 = 2500000.0
print(type(price)) # <class 'float'>
print(type(scientific)) # <class 'float'>
Be aware of floating-point precision:
print(0.1 + 0.2) # 0.30000000000000004
print(0.1 + 0.2 == 0.3) # False!
# For precise decimal arithmetic, use the decimal module
from decimal import Decimal
print(Decimal('0.1') + Decimal('0.2')) # 0.3
str)Text data — sequences of characters:
greeting = "Hello, World!"
name = 'Alice'
multiline = """This is a
multi-line string."""
print(type(greeting)) # <class 'str'>
bool)Logical values — either True or False:
is_active = True
is_logged_in = False
print(type(is_active)) # <class 'bool'>
# Booleans are also integers: True = 1, False = 0
print(True + True) # 2
print(False + 1) # 1
type() FunctionUse type() to check the type of any value:
print(type(42)) # <class 'int'>
print(type(3.14)) # <class 'float'>
print(type("hello")) # <class 'str'>
print(type(True)) # <class 'bool'>
print(type(None)) # <class 'NoneType'>
None is a special value that means "nothing" or "no value":
result = None
print(result) # None
print(type(result)) # <class 'NoneType'>
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.