Welcome to the How to Code in Python series β your beginner-friendly guide to learning how to code in Python from the ground up.
This post combines the first two lessons so you can quickly get up and running with your first Python script, understand basic data types, and interact with users through input.
Letβs jump in!
π οΈ Step 1: Installing Python
π» For Windows Users:
- Visit: https://www.python.org/downloads/
- Click βDownload Python [latest version]β
- On the setup screen, check:
β Add Python to PATH
- Click Install Now
- After install, open Command Prompt and run:
python --version
You should see something like:
Python 3.12.0
π§ For Linux Users (Ubuntu/Debian):
Run the following in your terminal:
sudo apt update
sudo apt install python3
Then confirm the install:
python3 --version
You should get:
Python 3.10.x or higher
π§ Most modern Linux distros already include Python!
βοΈ Step 2: Writing Your First Python Program
Create a file named hello.py and add this code:
# This is a comment
# Python ignores anything after a #
print("Hello, world!") # This prints to the screen
Run it:
πͺ On Windows:
cd path\to\your\file
python hello.py
π§ On Linux:
cd ~/path/to/your/file
python3 hello.py
Expected output:
Hello, world!
π You just wrote and ran your first Python script!
π§ Step 3: Variables and Printing
Letβs try working with variables.
Create a file called variables.py:
# Variables store data
name = "Angelus"
age = 29
location = "Chicago"
# Print the data
print("Name:", name)
print("Age:", age)
print("Location:", location)
Output:
Name: Angelus
Age: 29
Location: Chicago
β Variables are reusable and can store many types of data.
π£ Step 4: Python Data Types
Here are some core data types in Python:
# String (text)
greeting = "Hello"
# Integer (whole number)
year = 2025
# Float (decimal number)
temperature = 98.6
# Boolean (True or False)
is_logged_in = True
print(type(greeting)) # <class 'str'>
print(type(year)) # <class 'int'>
print(type(temperature)) # <class 'float'>
print(type(is_logged_in)) # <class 'bool'>
Understanding types helps you avoid errors and write clearer code.
β¨οΈ Step 5: Getting User Input
Letβs write a script that asks for your name and age.
Create a file called input_example.py:
# Ask the user for input
name = input("What's your name? ")
age = input("How old are you? ")
# Display the input
print("Nice to meet you,", name)
print("You are", age, "years old.")
Sample run:
What's your name? Alex
How old are you? 34
Nice to meet you, Alex
You are 34 years old.
π Note: The
input()function always gives you a string β even if the person types a number like25. That means Python sees"25"as text, not a number. If you want to do math with it, you have to convert it usingint()(for whole numbers) orfloat()(for decimals).
π Step 6: Converting Input to Numbers
Letβs do math with user input.
Create age_math.py:
# Ask for age
age = input("Enter your age: ")
# Convert string to integer
age = int(age)
# Calculate next yearβs age
next_year = age + 1
print("Next year, you'll be", next_year)
Example output:
Enter your age: 29
Next year, you'll be 30
If you skip the conversion, Python will throw an error:
TypeError: can only concatenate str (not "int") to str
β Recap: What You Learned
- β How to install Python on Windows and Linux
- β How to write and run your first Python script
- β Using variables to store data
- β Basic data types (string, int, float, bool)
- β
Getting user input with
input() - β
Converting strings to integers using
int()
Thatβs a ton of progress in just one post β be proud!
βοΈ Coming Up Next: If, Else, and Logic
In the next post, weβll dive into if/else statements, comparison operators, and how to let your code make decisions.
Stay tuned, and happy coding! π§βπ»
Leave a comment