Python Basics Beginner¶
When you'd use this
Variables, data types, operators and your first Python programs.
Reach for these fundamentals on day one of any script: declaring variables, converting user input, doing arithmetic, and formatting output.
What you'll learn¶
- Install Python and run your first script
- Understand variables and naming rules
- Work with all basic data types
- Use arithmetic, comparison and logical operators
- Get input from the user and format output
- Understand how Python executes code line by line
Variables & Data Types¶
Store values in named variables; every value has a type (str, int, float, bool, None). Use type() and conversions when data crosses boundaries like user input.
Python is dynamically typed — you don't declare types, you just assign values.
# Basic types
name = "Alice" # str
age = 30 # int
height = 1.75 # float
is_dev = True # bool
nothing = None # NoneType
# Check the type
print(type(name)) # <class 'str'>
print(type(age)) # <class 'int'>
Type conversion¶
x = "42"
print(int(x)) # 42 (str → int)
print(float(x)) # 42.0 (str → float)
print(str(123)) # '123' (int → str)
print(bool(0)) # False (int → bool)
print(bool("hi")) # True (any non-empty string is truthy)
Operators¶
Arithmetic, comparison, and logical operators are the building blocks of every expression. Note / always gives a float and // floors; comparisons/logic return booleans for conditions.
# Arithmetic
10 + 3 # 13
10 - 3 # 7
10 * 3 # 30
10 / 3 # 3.333... (always float)
10 // 3 # 3 (floor division)
10 % 3 # 1 (remainder)
2 ** 8 # 256 (power)
# Comparison — all return bool
5 > 3 # True
5 == 5 # True
5 != 4 # True
5 >= 5 # True
# Logical
True and False # False
True or False # True
not True # False
Strings¶
Text values with rich methods and slicing. Reach for f-strings to build output and .upper()/.split()/.replace() etc. for everyday text work.
# Creation
s1 = 'single quotes'
s2 = "double quotes"
s3 = """multi
line"""
# f-strings (use these — they're the modern way)
name = "Alice"
age = 30
msg = f"Hello, {name}! You are {age} years old."
# Common operations
s = "Hello, World!"
print(len(s)) # 13
print(s.upper()) # HELLO, WORLD!
print(s.lower()) # hello, world!
print(s.replace("World", "Python")) # Hello, Python!
print(s.split(", ")) # ['Hello', 'World!']
print(s[0:5]) # Hello (slicing)
print(s[-6:]) # World!
Input & Output¶
print sends text out; input reads a line in (always as a string — convert it immediately). The basics of any interactive script.
# Output
print("Hello")
print(f"Value: {42:.2f}") # Value: 42.00
# Input (always returns a string)
name = input("What is your name? ")
age = int(input("How old are you? ")) # convert immediately
Your first program¶
Putting it together: read input, compute, print output — the shape of countless small scripts.
# greeting.py
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print(f"Hello, {name}!")
print(f"In 10 years you will be {age + 10}.")
Run it:
Key rules to remember¶
Key rules to remember in Python Basics — what it is and when to use it.
Python Style
- Use
snake_casefor variable names:first_name, notfirstName - Use 4 spaces for indentation (never tabs)
- Lines should stay under 79 characters
- These are PEP 8 conventions — follow them from day one
Practice exercises¶
- Write a program that asks for two numbers and prints their sum, difference, product and quotient.
- Ask the user for their name and birth year, then print how old they are.
- Create variables of every data type and print them with
type(). - Write a temperature converter: Celsius → Fahrenheit using
F = C * 9/5 + 32.
💬 Discussion
Have a question about this topic? Found an error? Share your thoughts below.