Introduction

Python Beginners - Start Coding. This post will guide you through your first steps in creating a Python program. It assumes you have Python setup on your system or that you have a website you like for creating Python Code. If this is not the case, please read Python Beginners Guide Introduction. For this post, the IDE used will be Idle, which comes with Python. However, the code will work in any editor or IDE. It assumes you know how to open the editor and save a file. Once again, if this is not the case, please refer to the instruction article linked earlier. There is a video and written guide to opening Idle. There is also a video version available. Either scroll down or click here.

Hello World Program

The Hello World program is the defacto starting point for most programming tutorials. It outputs the words "Hello World!" to the screen. In Python, the command to do this is 'print'. Open Idle so you have the Shell and the Script Window side by side.

Python Idle with the shell window and script window side by side - Python Beginners Guide - Start Coding

Now, add the code below to the script window.


print("Hello World!")   

Press the F5 key to run the program or go to the Run menu, followed by Run Module. It should look like the image below.

Run Menu - Python Beginners Guide - Start Coding

Multiple Lines of Code - Python Beginners Guide - Start Coding

Okay, our next step is to increase the code to work with multiple lines. To do this, more print statements. Then run the code again. E.g.


print("Hello World!")
print("It is a great day to do some coding")

Idle script mode showing two lines of code and the output in the shell window - Python Beginners Guide - Start Coding

Variables - Storing Data

Variables are something we can create to store data. It is possible to think of variables as boxes. The box is a place to store the data with a label. Variables work the same way. We create a variable as a place to store data. We then give the variable a name that works like a label on a box. Finally, we assign data to the box, like placing items in a box.

Variables in code look like text in Python.


name = "Sara Payne"
print(name)

To use the variable, put it inside a print statement. Notice how the quotation marks are not there when using print with a variable. Once you have added the code into an Idle script window, run it and see the output. It should look like the image below.

image of python idle with a variable created then printed - Python Beginners Guide - Start Coding

Notice how the output is the data stored in the variable, not the variable's name.

More on Variables and the terminology - Python Beginners Guide - Start Coding

We now know how to store data in a variable and output the contents to the screen using the print command. Remember earlier I talked about creating a variable and then assigning a value to it? In the example above, we did this with a single line of code; however, it is possible to break this process down into stages.

  1. Create the variable
  2. Assign a value to it

name = None
name = "Sara Payne"

is precisely the same as


name = "Sara Payne"

Taking Input - Python Beginners Guide - Start Coding

The next stage is to take some input (information) from the user and store that in the variable. To do this, we are going to use the input command. We will create a variable and then assign the input taken from a user to the variable. There are a couple of essential things to note. Even at this basic level, there are multiple ways to write code that will produce the same behaviour. This is similar to combining the creation of a variable and the assignment seen above.

Try the code below. When you run the code, it should ask you for your name in the shell window. Type in your name and then press the enter/return key. It should then print out the name you typed in.


print("What is your name?")
name = input()
print(name)

idle with code showing input and print statements

Notice how the variable "name" stores the value the user enters. Then it is the contents of the variable that the print command uses. This behaviour is precisely the same as before, but it now takes input from the user and stores it in the variable. Finally, combining the print section of this code and the input command is possible to simplify it.

Combine the print and input commands.

Next, combine the print and input commands into a single line of code like the one below. This works similarly to combining creating a variable and assigning a value.


name = input("What is your name?")
print(name)

Python input simplified

Run the updated code, and the behaviour is almost identical. However, the position of the text in the shell window has changed. Functionally, this code is exactly the same. It takes a user's input, stores it and prints the stored value. The user experience isn't as good, though. It is possible to make some minor alterations to improve this code. The first way is to add a space between the question mark and where the user types their name. Alternatively, we can add a unique character representing a new line to look identical to the original input example. Try both examples below to see the difference.


name = input("What is your name? ")
print(name)


name = input("What is your name?\n")
print(name)

The first code example above put an extra space in by adding it inside the quotation marks. The second code example adds \\n, which is replaced by a new line. It is a unique character always present when you see a new line in a program, but it is invisible. In programs like Microsoft Word, it is possible to turn on the visibility for these characters.

Python input simplified and fixed to look nicer when the user is typing their input

Combine Variables and Other Text into a print command

So far, we have learned to create variables, print the contents, save user input into a variable and print the content. Next, we combine fixed text and multiple variables into a single print command. Similarly to everything else, there are multiple ways to achieve this. Variable contents can be combined, too.

Concatenation

Concatenation is just the technical term for joining strings. A string is the programming term for text. Put simply, concatenation takes two bits of text and joins them into a single bit of text. Try the code below and see what happens.


firstName = "Sara"
lastName = "Payne"
print(firstName + lastName)

You should see that the print statement outputs "SaraPayne" as a single bit of text. However, there will be no space in this text. There are several ways to add the space back. Essentially it is just adding a space inside the quotation marks. It is up to the coder where they think it's best placed. All three of the examples below will give the same output.


firstName = "Sara"
lastName = "Payne"
print(firstName + " " +  lastName)


firstName = "Sara "
lastName = "Payne"
print(firstName + lastName)


firstName = "Sara"
lastName = " Payne"
print(firstName + lastName)

In the first example, the space character is in the middle of the print statement. Differently, in the second example, it is at the end of the string called firstName. Finally, the third example is before the start of the string called lastName. It is also worth noting that variables can be combined this way as long as they contain text (they are strings). The following example concatenates the first two variables into a third variable called fullName. Then, the contents of the variable fullName are printed.


firstName = "Sara"
lastName = "Payne"
fullName = firstName + " " + lastName
print(fullName)

Learning to join strings using concatenation is better because this method will work almost universally in any programming language. It is not restricted to Python. However, as always, there is an alternative discussed below.

Combine output in a print statement using commas.

This method of joining strings will only work inside a print statement. It can not be used to create a new variable in the way concatenation can. However, it does remove the necessity of handling spaces. In later tutorials, it also avoids other complications in the name of simplicity. These guides will show both methods and explain why simplicity may not be a good idea. Try the following code.


firstName = "Sara"
lastName = "Payne"
print(firstName, lastName)

You should see the output "Sara Payne" in the shell window.

Combining variables and other text in the print command

Combining variable data with fixed text in a print statement is also possible. With concatenation, it is possible to do this and create a new variable. See the code below.


name = input("What is your name\n")
age = input("How old are you\n")
print("Your name is: " + name + " and you are " + age)

This code takes two inputs and stores them into separate variables. Next, the contents of the variables are combined with other strings (text) in the print statement using concatenation. It is worth noting where the spaces are added inside the quotation marks.

concatenating strings in python using idel

It is also possible to write this code using commas.


name = input("What is your name\n")
age = input("How old are you\n")
print("Your name is:", name, "and you are", age)

The command version looks tidier and is arguably easier to remember. However, it will only work with Python and doesn't allow the creation of new variables. See the final examples for combining text below.


name = input("What is your name\n")
age = input("How old are you\n")
firstText = "Your name is: "
secondText = " and you are "
output = firstText + name + secondText + age 
print(output)

Finally, there is an alternative way to concatenate text in a string variable with the plus operator. See the example below; it will give the same result.


name = input("What is your name\n")
age = input("How old are you\n")
output = "Your name is:"
output += " "
output += name
output += " "
output += "and you are"
output += " "
output += age
print(output)