To get Python to put things on the screen you use the keyword print this is all lowercase and if done correctly it will turn pink in IDLE. All 'doing' things in Python are followed by a normal pair of brackets ( ), we put the thing we want to print in the brackets. As computers usually deal with numbers if we want to print text we need to put it in speech marks. You can use either the double quotes " or single quotes ' so long as you use the same one at the start and end of the text.
To print Hello, I like cake. we would type in:
print("Hello. I like cake.")
Save the file, then run it by going to the Run menu and clicking on Run Module in IDLE, or by pressing f5. You should see the text appear in blue in the IDLE shell window. Well done, your first Python program. :-)
You may be thinking what's the point of writing a program to print stuff, it takes longer to do that than just typing it up in a Google Doc !! You have to keep adding all these print and brackets and speech marks - and you'd be right if that is all it can do. BUT we can do so much more.
We can put labels on things and then use those labels in our print commands. For example if we store some data about a rabbit like this:
animal_name = "rabbit"
number_legs = 4
size = "small"
diet = "grass, hay and a small amount of carrots"
hide = "soft fur"
lives_for = "8 to 12 years as a pet"
We can then print it out like this:
print("Animal name:", animal_name)
print("This", size, "animal has", number_legs, "legs")
print("A", animal_name, "eats", diet)
print("It is covered in", hide, "and lives for", lives_for)
Normal bits of text go inside speech marks and we can use the labels we made earlier to add the specific information in. Each bit is separated by a comma.
The above program will put this on the screen
Animal name: rabbit
This small animal has 4 legs
A rabbit eats grass, hay and a small amount of carrots
It is covered in soft fur and lives for 8 to 12 years as a pet
Now you are thinking 'This is EVEN LONGER!!! ' and it is, but we can now re-use the print part and just change the data stored in the labels to print out information on different animals. This idea of using one general piece of code to do lots of different things over and over again is at the heart of most programs. If we had to write a whole different program for each different animal then nobody would bother. Imagine a calculator that had a button for 1+3 and a different button for 4+2 and another button for 9-3. It certainly wouldn't fit in your pcoket. Look for opportunities to use this idea in your programs.
To get the user to type stuff in from the keyboard we use the input( ) command. We put the prompt - the instruction the user is following - inside the brackets, just like we did with the print command.
input("What is your name ? ")
This line will put the text 'What is your name ? ' in the IDLE shell and pause the program whilst the cursor flashes at the end of the line, waiting for the user to type something and hit enter. The trouble is whatever the user types in is instantly forgotten by the program as it moves on to the next instruction. We haven't told the program what to do with the letters from the keyboard. What we need to do is give this information from the keyboard a label so we can use it again later. So we should write:
username = input("What is your name ? ")
The program will do the same as before but it will now remember what the user types in and it will remember that the thing that was typed in is called 'username' and it will know where to find it again later if we need it.
You don't have to use 'username' as the label. You could use 'name', or 'first_name', or 'client', or 'user'. The computer doesn't know what a 'name' is, it is just a collection of characters so it can remember what was typed in. It's a good idea to use relevant labels so we puny humans can remember what they refer to. You could use 'Zb4y8' (which would be very hard to remember) as the label or 'banana' (which would be confusing for other people reading your code).
There are a couple of rules about what you can use as labels. In Python labels can only contain letters (both upper and lowercase), numbers (0-9) and the underscore character ( _ ) , and they can't start with numbers. So number_of_10cents is fine as a label but 10cent_coins is not.
Technically these labels are called identifiers. In the example above the label is being used to store a name, that name can change or vary each time the program is run, so these identifiers are often called a variables.
Can only have upper and lowercase letters, numbers, and the underscore character in them.
They can't start with numbers.
We can combine these two ideas by asking the user for their name, storing it as a variable and then using it in a greeting like this:
username = input("What is your name ? ")
print("Hello,", username)
In the print bracket we can add different pieces of text together just by separating them with a comma, and you can use as many pieces of text and variables as you like.
The input bracket can only take one piece of text. We can add two (or more) pieces of text together to make a single piece of text using a process called concatenation (con - cat - ten - nation). Instead of a comma we use a + so we can write:
feeling = input("How are you feeling today " + username + "? If you don't mind me asking.")
***Note: the comma adds a space between each bit of text whilst the + does not, so we have to add our own spaces if we concatenate.
More Printing
Storing Stuff
Input
These videos below are for people using IDLE
Video recapping the print output command
Video recapping the input command