Lesson 1.3: First Python Program
In the previous lesson, we learned about IDLE and Python shell, and how to start and use both of them. We are now finally ready to write our very first Python program called “Hello”. Yeah!
Create a new Python file
- Start IDLE the same way you did in Lesson 1.2.
- In IDLE, click File > New File, or just push Ctrl+N. This will open a new window named Untitled.
- Click inside the Untitled window and type
- Now click File > Save As… or push Ctrl+Shift+S at the same time. This will open a Save-As dialog.
- Navigate to your Documents folder, inside it, create a new folder called CodeWithSara, inside it create another folder called Python101, and inside it create yet another folder called Unit1.
- Save the file in the Unit1 folder and name it hello.py.
The print() function
The statement print(“Hello1”) uses a built-in function print(). This function outputs whatever value is passed to the function as a parameter, in this case “Hello1”.
There are many other built-in Python functions. The two things you need to know about functions right now is that functions have to be called like this: function_name() and that some function accept parameters which must be included between ( and ).
Running your program
On the hello.py window, click on Run > Run Module or simply press F5 (You may need to hold the Fn button or similar at the same time).
Now look at the Python shell window.
It contains the result of running hello.py! The first line (starting with === RESTART ) is just telling us which Python file was executed. The other two lines are the actual output of the program, in our case Hello1 and Hello2. And that’s exactly what we asked our first program to do – print Hello1 and then print Hello2!
Making hello.py better
Let’s make our program a bit more friendly. How about if instead of just saying Hello, we say Hello, Sara! That’s easy, you say? Let’s make it a bit more challenging and add some variables. This will make it easier to read the program and later modify it even further.
Sequence of characters enclosed in ‘ or “ is called a string. Fortunately for us, string is just another type of values, which means that we can assign a string to a variable. Let’s use a variable name for Sara’s name and a variable greeting for the entire greeting. Also, we can concatenate (two or more) string using the + operator.
We can now update the hello.py program to look like this:
Experiment with this program, modify it to use your name, etc. Also, notice that missing or extra spaces within strings affect the output of the program whereas missing or extra spaces outside strings (for example between + and name) do not affect the final output.
Final version of hello.py
You can modify the program above to output Hello, Joe! and impress your friend Joe. You can modify it again and impress your friend Jane. But quickly, you get tired of changing the program over and over. Isn’t there some better way?
Thankfully, Python has a built-in function called input() which prompts a user to type something in the Python shell and then returns that string to the program that called it. Typically, the return value is then stored in a variable and used later in the program.
We can use this function in our program to prompt user for his or her name and then greet them back by their own name. Now that’s cool!
In the screenshot above, you can see both the source code of the final program and the interactions in the Python shell:
- User runs the program using the Run menu or pushing F5.
- Python outputs Please enter your name: into the Python shell.
- User types Monty into the shell.
- Python outputs Hello, Monty!
Create a new program named hello_with_last_name.py that prompts users for their first name and their last name. Store their first name in a variable called first_name and their last name in a variable called last_name. Store the entire greeting in variable called greetings. Then output the final greeting in the form Hello, <first_name> <last_name>! How are you?
Congratulations! You have written and run your very first Python program! And your program is interactive; i.e. it changes its behavior based on user input. Great job!