Python Coding Course: Your First Python Program



Python Coding Course: Your First Python Program

Welcome back, aspiring Python programmers! In our last lesson, we introduced you to Python and gave you a sneak peek at a simple program. Today, we're going to dive deeper into that program and understand exactly what's happening. 


Runnable Code Snippet

Python Example


print("Hello, World!")
print("Welcome to Python Programming!")
            


Understanding the Code

Our program consists of two lines.  Each of these lines is doing something very specific. Let's examine them in detail.


The print() Function

In Python, print() is a built-in function that outputs text to the screen. It's one of the most basic and frequently used functions in Python. When you want to display something in Python, you use the print() function. The content you want to display goes inside the parentheses.


String Literals

In our example, we're printing what we call "string literals". In programming, a string is a sequence of characters - essentially, a piece of text. In Python, we create strings by enclosing text in quotation marks. For example, "Hello World!" is a string. The quotation marks tell Python that everything between them should be treated as text.


Program Execution

When you click the "Run" button, Python executes these instructions one at a time, from top to bottom. First, it sees print("Hello World!") and displays "Hello, World!" on the screen. Then, it moves to the next line and sees print("Welcome to Python Programming!"), so it displays that message. This is why you see two lines of output when you run the program.


Experiment Time!

Now that you understand what each part of the program does, it's time to experiment! We'll use OnlineGDB, a free online Python compiler and debugger, to modify and run our code. 


Setting Up OnlineGDB

First, you'll need to set up an account on OnlineGDB. Go to https://www.onlinegdb.com/ and click "Sign Up" in the top right corner to create a free account. This will allow you to save your work. Once logged in, click "Create New Project" to start a new coding session.


Copying and Running the Code

Now, let's get our code into OnlineGDB. Click the "Copy Code" button in the snippet above to copy the "Hello, World!" program. In OnlineGDB, make sure "Python" is selected in the language dropdown menu in the upper right corner of the IDE. Then, paste the code you copied into the editor. Click the "Save" button (floppy disk icon) in the top menu, give your project a name like "HelloWorldExperiment", and click "Save". To run the code, click the green "Run" button at the top of the page. You should see the output in the console below the editor.


Modifying the Code

Here's where the fun begins! Try making some changes to the code in the OnlineGDB editor. You could change the text inside the quotation marks to make it say hello to you by name. Or try adding another print() line with a message of your choice. You could even experiment with removing one of the print() lines to see what happens.


After each modification, don't forget to click the "Run" button again to see how your changes affect the output. Remember, programming is all about experimenting and learning from the results. Don't be afraid to make changes and see what happens!


Why "Hello, World!"?

You might be wondering why we start with a "Hello, World!" program. This tradition goes back to 1978 and has been a standard first program for learning new programming languages ever since. It's simple, yet it teaches us several important concepts: how to use a basic function like print(), how to work with strings, and how a Python program executes line by line. Plus, it's encouraging to see immediate results when you're just starting out!


What's Next?

In our next lesson, we'll explore variables - a fundamental concept in Python that allows us to store and manipulate data in our programs. We'll see how we can use variables to make our programs more flexible and powerful.


Remember, every journey begins with a single step, and you've just taken your first step into the world of Python programming. By saving your project in OnlineGDB, you can come back to it later and continue experimenting. This platform will be useful throughout our course for trying out code and working on exercises.


Keep practicing, stay curious, and happy coding!



Image: RealToughCandy from Pexels

Comments

Popular posts from this blog

The New ChatGPT Reason Feature: What It Is and Why You Should Use It

Raspberry Pi Connect vs. RealVNC: A Comprehensive Comparison

The Reasoning Chain in DeepSeek R1: A Glimpse into AI’s Thought Process