Day 3: Writing Our First Python Program in Detail
Welcome back to Master Python in 100 Days! Today, on Day 3, we are going to write our very first Python program from scratch, step-by-step. This isn’t just about writing code; it’s about understanding every line and making sure we know exactly what’s happening in our program. Let's get started!
A Quick Review: What We Did on Day 1
If you remember, on the very first day of the Master Python in 100 Days, we touched upon how to use the print()
function to display basic output on the screen. That was a warm-up, where we gave a quick look at the print()
function.
But starting from today, we will dive deeper into the actual use of print()
. By the end of this session, you’ll not only understand how to use print()
properly, but also learn how to avoid common errors and use it in your own Python scripts. So, let’s take our Python learning journey one step further!
Introduction to the print()
Function :
The very first thing you need to know about Python is the print()
function. Functions in Python are used to perform specific tasks. When we use the print()
function, we are telling Python to display information on the screen. Here's how it works:
Here’s an example:
Explanation:
print
: This is the function. In Python, functions are tools that perform tasks."Hello, World!"
: This is a string. A string is a sequence of characters (text) that is enclosed within either double quotes (" "
) or single quotes (' '
). Python recognizes text as a string when it is inside quotes.
print
: This is the function. In Python, functions are tools that perform tasks."Hello, World!"
: This is a string. A string is a sequence of characters (text) that is enclosed within either double quotes (" "
) or single quotes (' '
). Python recognizes text as a string when it is inside quotes.How It Works:
The print()
function is very useful for checking the output of a program, debugging, and displaying results.
Common Mistakes and Errors :
When you’re learning to code, making mistakes is natural! Let's see a common mistake people make with the print()
function:
- Python doesn't know what
Hello World
is without the quotes. Python thinksHello World
is some kind of variable or command, but since it’s not defined, it gets confused.
- Solution: You need to enclose
Hello World
in quotes to tell Python it's a string:
REPL vs Script
When coding in Python, you have two ways to run your code:
1. REPL: This allows you to type one command at a time and get instant feedback (more on this in Step 4).
2. Script: You can write multiple lines of code in a file, and Python will execute them all at once. This is much more efficient for larger programs.
Opening REPL or Script
1. Using REPL (Read-Eval-Print Loop):
REPL is an interactive Python environment that stands for:
- Read: It reads the command you enter.
- Evaluate: It evaluates (or runs) the command.
- Print: It prints the result of the command.
- Loop: It loops back to allow you to enter another command.
- Open your terminal or command prompt.
- Type
python
orpython3
(depending on your installation) and press Enter. This will start the Python interpreter in REPL mode, where you can type and execute Python commands one at a time.
This is useful for quickly testing code or learning new concepts. You can test simple commands like addition, string printing, etc., without having to write an entire script.
Why Use REPL?
- While REPL is great for testing small pieces of code, it’s not ideal for writing longer programs. That’s where scripts (like
main.py
) come in handy.
2. Using a Script:
- Open your text editor (like VS Code or any other you prefer).
- Create a new file and save it with a
.py
extension, for example,main.py
. - You’ll write your code in this file and run it all at once by executing the script from the terminal with
python main.py
.
Let’s create a simple Python script where we print multiple things:
print("Hello, World!")
: Prints the string"Hello, World!"
.print(5)
: Prints the number5
. Notice we don’t need quotes around numbers.print("Goodbye!")
: Prints the string"Goodbye!"
.
The Output:
"Hello, World!"
, then it prints 5
, and finally it prints "Goodbye!"
.Why Scripts Are Better:
- Scripts are useful when you want to run a series of commands without typing them over and over again. You write the code once, and Python executes it step by step. This is particularly handy when dealing with larger programs.
Using Python to Perform Calculations :
Python is not just limited to printing text. It’s a powerful language that can also perform arithmetic operations. Let’s see how Python can handle some basic math:
Explanation:
print(17 * 13)
: This multiplies17
by13
and prints the result. You’ll see221
as the output.print(221 / 2)
: This divides221
by2
and prints110.5
.
In Python:
*
is used for multiplication./
is used for division.+
is used for addition.-
is used for subtraction.
print()
Function: Used to display information on the screen.- Syntax Errors: Common errors, such as forgetting quotation marks, can be easily fixed.
- Scripts: Useful for running multiple lines of code at once.
- REPL: Great for quick, interactive coding and testing.
- Arithmetic: Python can easily handle math operations.
On the first day, we briefly touched on print()
, but today we’ve taken a much deeper dive. Practice these concepts, and tomorrow we will move on to more exciting Python features!
Thank you for joining me on Day 3 of the "Master Python in 100 Days" challenge. Together, we are building a strong foundation in Python, and I look forward to sharing more valuable lessons in the days to come.
Let’s continue growing, learning, and coding together!
Comments
Post a Comment