More Code Notes
  • Home
  • Notes
  • Python
    • CSV I/O in Python
    • Hello World
    • Creating a class in Python
  • C/C++
    • Casting in C++
    • Copying in C++
    • CSV I/O in C++
    • How to use memset() in c++
  • About
  • Home
  • Notes
  • Python
    • CSV I/O in Python
    • Hello World
    • Creating a class in Python
  • C/C++
    • Casting in C++
    • Copying in C++
    • CSV I/O in C++
    • How to use memset() in c++
  • About

Creating the first "Hello, World!" Program


    
Here it is, the first program most of you will start your code journey with. This program can be even simpler, but it's broken out so you can see what parts need to go into this python code.

First, we have the line # file: HelloWorld.py
This line is a comment, meaning that when we run this program, the line will be ignored and not checked to see if there are special code words needed to complete the program. This line is for you, the programmer, to take notes and remind yourself of information later down the line when you look at your program again. Sometimes(Actually, a lot of the time) you can forget why you wrote a line of code in a certain way. Comments will help you get back on track quickly.

Next, we have the line: if __name__ == "__main__":
This is an if statement. Generically, the if statement contains the word 'if' and a conditional statement. This conditional statement needs to evaluate to either true or false. In this case, the conditional statement is __name__ == "__main__" and it is followed by a colon, :, to indicate the lines of code after this if statement will only be run if the conditional statement is true. In this case, the condition is something unique to Python. To put is simply for now, The program to run is this 'main' file above. The Python interpreter recognizes that this is the main file and will see this if statement as true. This is the entry point to the rest of the program, and how many simple programs written in Python will start!

Lastly, we have two lines of code: aString = "Hello, World!" and print(aString)
This is a variable. Simply put, a variable is a container of sorts. The container can be filled with data and changed later on in the program, if desired. In this situation, the variable is 'aString' and the data is "Hello, World!".
In this program, we set the variable with data which allows us to call the variable in the second line of code. This basically moves the container, full of data, over to the function print(). This function then works with the container full of data to print out the words "Hello, World!" to a Command Line or Terminal window.

To make this code work, open a text editor and copy the code above to a new file. Save the file as "HelloWorld.py" and put it somewhere you can remember.

Once it is saved, open the Command Prompt(on Windows) or Terminal(on mac)

Run the command
python path/to/file/HelloWorld.py
  • Where 'path/to/file' is the location where the HelloWorld.py file is saved

This should print out the following result:
Picture

And there you go! Your first program. It doesn't do much now but it is a solid foundation to some very handy concepts that will help a ton as you continue programming.
  • Home
  • Notes
  • Python
    • CSV I/O in Python
    • Hello World
    • Creating a class in Python
  • C/C++
    • Casting in C++
    • Copying in C++
    • CSV I/O in C++
    • How to use memset() in c++
  • About