Python Programming From Zero to Hero
Python Basics
Setting Up Your Workspace
Before you can write Python code, you need a place to write and run it. This is called your development environment. The first step is to install Python itself from the official website, python.org. The installation is straightforward and includes a basic tool called IDLE, which is great for starting out.
As you write more complex programs, you'll likely want a more powerful tool called an Integrated Development Environment (IDE) or a code editor. Tools like Visual Studio Code, PyCharm, and Sublime Text offer features like syntax highlighting (which colors your code to make it easier to read) and debugging tools. For now, the included IDLE is all you need.
Your First Python Program
It's a tradition for a programmer's first program to simply display the text "Hello, World!" on the screen. In Python, this is incredibly simple. We use a built-in command called a function. The function for printing output to the screen is print().
print("Hello, World!")
That's it. You write the word print, followed by parentheses. Inside the parentheses, you put the text you want to display, wrapped in double quotes. The quotes tell Python that this is a piece of text, not a command to be interpreted.
Python's syntax is designed to be clean and readable. There are no complicated symbols or extra words needed just to get this simple task done.
Variables and Data Types
Programs need to store and manage information. We do this using variables. Think of a variable as a labeled box where you can keep a piece of information. You give the box a name (the variable name) and put something inside it (the value).
To assign a value to a variable, you use the equals sign =.
# Assigning the value 30 to a variable named 'age'
age = 30
# Assigning the text "Python" to a variable named 'language'
language = "Python"
The information we store comes in different forms, or data types. Python automatically understands the type of data you assign. Let's look at the three most common basic types.
string
noun
A sequence of characters, used to represent text. Strings are enclosed in single (' ') or double (" ") quotes.
Strings are the lifeblood of any program that deals with text. Usernames, messages, and labels are all stored as strings.
user_name = "Alex"
integer
noun
A whole number, positive or negative, without any decimal places.
Integers are used for counting things, like the number of items in a shopping cart or a player's score in a game.
items_in_cart = 5
float
noun
A number that contains a decimal point. The name comes from "floating-point number."
Floats are essential for any calculations involving fractions or precise measurements, like prices, distances, or scientific data.
price = 19.99
Getting User Input
A program is more interesting when it can interact with a user. Python's input() function lets you pause the program and wait for the user to type something.
You can provide a message inside the parentheses to prompt the user for what you want them to enter. The function then takes whatever the user types and gives it back to your program as a string.
# Prompt the user for their name and store it in a variable
name = input("What is your name? ")
# Greet the user by name
print("Hello, " + name + "!")
An important thing to remember: the
input()function always gives you back a string, even if the user types in a number.
This means if you ask for a user's age and they type 25, the input() function will return the string "25", not the integer 25. We'll explore how to convert between data types later on.
Which line of code will correctly display the text 'Welcome!' on the screen?
In programming, what is the primary role of a variable?
You've just taken your first steps into the world of Python. You've set up your environment, written a program, and learned how to handle basic data and user input. These are the fundamental building blocks for everything that comes next.