By the end of the lesson I will be able to:
From a previous lesson: A compiler is a computer program that translates a language into binary, which can be read by the computer.
The language we are using is Python. Python is considered a "High Level Language" which means it appears more like the English language than computer language. Lines of code in High Level Languages are written so that they can be read out loud and be understood easily. In general, all High Level Languages offer similar features, the only thing that changes is the syntax. Programming syntax is the set of rules that determine which words (known as symbols) can be combined to make a sentence that is readable by the compiler.
One of the most important things we can do as beginner programmers is learn how to output information to the screen and get input from the user. We have been doing this in demonstrations already so the concept should not be too difficult to follow.
To output text to the screen in Java we use the following command:
print(output)
Where output is the data that will be put out to the console. Remember, the console is the sub-window in the bottom right of WingIDE 101.
Example:
print("Hello World")
print(42)
The above two lines of code will print the string "Hello World" and the integer 42 to the console.
On the right we see a picture of the console.
Getting input from the user in Python requires a different command. The command used, depends on which version of Python you are running.
In Python 2.x.x we use the following:
raw_input("prompt")
In Python 3.x.x we use:
input("prompt")
Each of the above lines will print the prompt on the screen, then wait for the user to type in something. The program will continue after the user presses enter. A prompt is used to tell the user what to do. For example if we want the user to type in their name we would write:
# Python 2.x.x
raw_input("Type your name here, then press enter:")
# Python 3.x.x
input("Type your name here, then press enter:")
Prompts are very important, and you should always have one when the user is required to input information. A prompt should always be either a question to be answered or request for information from the user.
What happens when you want to USE the information typed in by the user? Currently we are not able to use the user input because it is being ignored. We now have to store that input in a variable in order to use it later.
name = input("Type your name here, then press enter: ")
The data typed by the user is stored into a variable called name, at which point we can use it later.
This program asks for the user to enter their name, then says hello to the user. Below is the output
You will usually want to do more with the input than simply saying hello, but it's a good place to start!
In the above program, notice the + between "Hello " and name. This is a special use of the symbol, called concatenation, which is the process of joining 2 strings together. By default you can't always do this. Try to run the following code:
print ("Hello " + 1234)
You will end up with the following:
We see that you cannot concatenate 'str' and 'int' objects. This means that by default, Python you cannot put a String and integer together using the + concatenation. We need to convert the integer into a string first!
The process of converting values between types is called casting.
Casting primitive types (int, float, Boolean) to strings in Python is simple. The str() function does it all for us! Let's convert the previous line so that we can concatenate the string and number together:
print("Hello " + str(1234))
If you run this code, you should see that it works! Notice that we wrapped the 1234 in some brackets and the str keyword. This will attempt to convert anything into a string.
Printing integers to the console is useful, but it's only the tip of what you need to be able to do. Say, for example you wanted to find the sum of 2 numbers entered by the user:
n1 = input("Enter the first number: ")
n2 = input("Enter the second number: ")
print (n1 + n2)
In this case, both inputs are treated as strings! So that + concatenates the two values rather than adding them.
Similar to casting to strings, we can use the int() function to cast a string to an integer:
n1 = input("Enter the first number: ")
n2 = input("Enter the second number: ")
print (int(n1) + int(n2))
These changes produce the desired result! Both values are treated as integers and added together.
It's up to you, the programmer, to determine what types you need and properly convert values whenever necessary.
Create the following programs as review of the material above and previous lessons: