Python Mastery
Python Basics
Setting Up Your Space
Before you can start writing Python, you need a place to do it. This is called a development environment. It's simply the set of tools you use to write and run your code. The first step is to install Python itself from the official website, python.org. The installation is straightforward and comes with a basic but powerful tool to get you started: IDLE.
IDLE (Integrated Development and Learning Environment) gives you two main things: a shell for running single lines of code, and an editor for writing longer programs, called scripts.
Let's write our first program. It’s a tradition in programming to start by making the computer say "Hello, World!". In Python, this is incredibly simple. Open IDLE, and in the shell window (the one with the >>> prompt), type the following and press Enter:
print("Hello, World!")
The computer will immediately respond by printing Hello, World! on the next line. You’ve just written and run your first piece of Python code. This print() command is a built-in function that tells Python to display whatever you put inside the parentheses.
Python's Building Blocks
Like any language, Python has grammar rules, which we call syntax. The good news is that Python's syntax is designed to be clean and readable. You've already seen it in action with print("Hello, World!"). The text is enclosed in double quotes, and the whole thing is placed inside the parentheses of the print function.
One of the most fundamental concepts in programming is the variable. Think of a variable as a labeled box where you can store information. You give the box a name, and you can put things in it, take them out, or replace them with something else.
To create a variable, you just choose a name, use the equals sign (
=), and provide the value you want to store.
# Let's create a variable named 'greeting'
# and store the text "Hello there" in it.
greeting = "Hello there"
# Now we can print the content of the variable
print(greeting)
Variables can hold different types of information. These are called data types. For now, we'll focus on the four most common ones.
integer
noun
A whole number, without any decimal points.
Integers are used for counting things, like the number of users or the score in a game.
float
noun
A number that has a decimal point.
Floats are useful for representing quantities that can be fractional, like measurements or prices.
string
noun
A sequence of characters, like a word or a sentence. It's always surrounded by quotes.
Strings are how we work with text in Python.
boolean
noun
A type that can only have two values: True or False.
Booleans are essential for making decisions in our code.
Making Decisions and Repeating Actions
So far, our code has run straight from top to bottom. But the real power of programming comes from controlling this flow. We can tell our programs to make decisions or to repeat actions.
To make decisions, we use conditional statements. The simplest is the if statement. It checks if a condition is True, and if it is, it runs a block of code. You can also provide an else block to run if the condition is False.
temperature = 15
if temperature > 25:
print("It's a hot day!")
else:
print("It's not so hot.")
# The output will be: It's not so hot.
Notice the indentation. In Python, the spaces at the beginning of a line are important. They tell Python which lines of code belong to the if block and which belong to the else block.
What if we want to do something over and over again? That's where loops come in. A for loop is perfect for repeating an action a specific number of times.
# This loop will run 5 times.
# The range(5) function generates numbers from 0 to 4.
for number in range(5):
print("This is loop number", number)
This simple loop prints a message five times, each time updating the value of the number variable. Loops are incredibly useful for working with lists of items or just automating repetitive tasks.
Now, let's test your understanding of these fundamental concepts.
What is the name of the integrated development environment that comes bundled with a standard Python installation?
Which line of code will correctly print the text Ready to code! to the screen?
These are the essential building blocks of Python. With variables to store data, and control structures to direct the flow, you can start to write simple but useful programs.
