Python is one of the most beginner-friendly programming languages in the world. Its syntax is close to plain English and you can run code instantly without a complicated setup. Here's everything you need to write your first real Python program.
Running Python
You can run Python right here on BaseCodeByte without installing anything. If you want to run code locally, install Python from python.org and run scripts with: python3 yourfile.py
Variables
Variables store values. In Python you don't declare types — Python figures them out:
name = "Alice"
age = 25
height = 1.75
is_student = TruePrinting output
print("Hello, world!")
print(name)
print(f"My name is {name} and I am {age} years old.")The f"..." syntax is called an f-string. It lets you embed variables directly in strings.
Getting input
name = input("What is your name? ")
print(f"Hello, {name}!")Conditionals
age = 20
if age >= 18:
print("Adult")
elif age >= 13:
print("Teenager")
else:
print("Child")Python uses indentation (4 spaces) to define code blocks. There are no curly braces.
Loops
for i in range(5):
print(i) # prints 0 1 2 3 4
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)Functions
def greet(name):
return f"Hello, {name}!"
message = greet("Alice")
print(message)Lists
numbers = [10, 20, 30, 40]
numbers.append(50)
print(numbers[0]) # 10
print(len(numbers)) # 5Your first real program
Put it all together — a simple number guessing game:
import random
secret = random.randint(1, 10)
guess = int(input("Guess a number (1-10): "))
if guess == secret:
print("Correct!")
elif guess < secret:
print("Too low!")
else:
print("Too high!")That's enough to write real, useful Python programs. From here, explore lists, dictionaries, file reading, and then pick a project that interests you.