Skip to main content

Day 3: Writing Our First Python Program in Detail | Master Python in 100 Days


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:

  • printThis 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:

When you run this code, Python will take whatever is inside the parentheses and display it on the screen. In this case, it will output:

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:





What’s Wrong Here?
If you run this, Python will return a SyntaxError. Here’s why:


  • Python doesn't know what Hello World is without the quotes. Python thinks Hello 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:




Creating a Python Script :

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.

How to open REPL :

  • Open your terminal or command prompt.
  • Type python or python3 (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.
In REPL, you can type a Python command, and Python will immediately give you the output. Here’s an example:



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:


What’s Happening Here?
  • print("Hello, World!"): Prints the string "Hello, World!".
  • print(5): Prints the number 5. Notice we don’t need quotes around numbers.
  • print("Goodbye!"): Prints the string "Goodbye!".


The Output:


When we run this script, Python will execute it line by line. This means it first prints "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 multiplies 17 by 13 and prints the result. You’ll see 221 as the output.
  • print(221 / 2): This divides 221 by 2 and prints 110.5.

In Python:

  • * is used for multiplication.
  • / is used for division.
  • + is used for addition.
  • - is used for subtraction.


You can use these symbols to perform any mathematical operation you need. In this case, Python acts like a calculator!


Key Points :
  1. print() Function: Used to display information on the screen.
  2. Syntax Errors: Common errors, such as forgetting quotation marks, can be easily fixed.
  3. Scripts: Useful for running multiple lines of code at once.
  4. REPL: Great for quick, interactive coding and testing.
  5. 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

Popular posts from this blog

Day 5: Understanding Variables and Data Types in Python | Master Python in 100 Days

Day 5: Data Types, Variables, and Type Conversion in Python W elcome to Day 5 of our "Master Python in 100 Days" journey! Today, we will explore some of the most important building blocks in Python: data types , variables , and type conversion . Understanding these concepts is essential as they form the basis of how we store and manipulate data in our programs. What are Data Types? Data types tell Python what kind of data we are working with. Python has several built-in data types, and the most common ones are:   1.  Integers (int) : These are whole numbers without decimal points, like 10 , -5 , 0 , etc .   2.  Floats (float) : These are numbers with decimals, like 3.14 , 0.001 , -2.5 .  3. Strings (str) : A string is a collection of characters (letters, numbers, symbols) enclosed in quotes ( " " or ' ' ) . These are used to represent text.  4.  Booleans (bool) :  These are either True or False values. It’s used for logical conditions. Chec...

Day 1: Introduction to Python | Master Python in 100 Days

Day 1: Introduction to Python Hello everyone! Welcome to Day 1 of our 100 Days to Learn Python journey! Today, we will start by understanding what Python is, its importance, and how to get started. What is Python? Python is a popular, high-level programming language that was created by Guido van Rossum and first released in 1991 . It’s designed to be easy to read and simple to write, making it perfect for both beginners and advanced programmers. Python allows developers to focus on solving problems rather than worrying about the complex syntax often found in other programming languages. Why Learn Python? 1. High demand: Many companies need Python programmers. 2. Versatile: You can use Python for web development, data analysis, machine learning, game   development, automation, and more. 3.  Great community: There are many resources, forums, and libraries available online.    Key Features of Python: 1. Simple and Easy to Learn : Python’s syntax is similar to the...