🐍 How to Code in Python: Getting Started β€” First Program, Variables, and User Input

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:

  1. Visit: https://www.python.org/downloads/
  2. Click β€œDownload Python [latest version]”
  3. On the setup screen, check:
   βœ” Add Python to PATH
  1. Click Install Now
  2. 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 like 25. That means Python sees "25" as text, not a number. If you want to do math with it, you have to convert it using int() (for whole numbers) or float() (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