As with all programming languages, coders use an editor to enter statements. Some editors also have the ability to run or execute the statements.
For your first assignment, you will use the IDLE code editor that comes with Python. IDLE has a "Shell" that runs Python statements immediately. To start: find and open IDLE. The interface will look something like this:
By entering a valid Python statement and pressing [Enter], Python will "interpret" and execute the statement immediate. For example,
print("My name is David")
The print() statement is a built-in Python function that displays the output to the screen. All functions require parenthesis and print() accepts zero, one, or more values (arguments) inside the parenthesis to tell print() what to display.
It is permissible to use print() without any value(s), in which case print outputs only a carriage-return or next-line character and the display advances to another line. Using print("") or print(" ") (empty quotes or just spaces) will create the same effect.
The Python interpreter will ignore attempts to use print without parenthesis. Leaving out one quote mark or a parenthesis will cause the Python interpreter to "throw an exception". Try it.
EOL means "End of Line".
Literals and Variables
The value(s) provided to the print() function can be literals or variables. A literal is an actual value such as "Hello", "Sam Smith" 45 or 3.45. A variable is a storage container you define with a name, such as newGreeting, MyName, user_age or avg_Score. In Python, you declare a variable by assigning a value to it, such as
MyyName = "Sam Smith"
More Ways to Use print()
The print() statement is very versatile. Multiple values can be supplied inside the parenthesis. Values supplied to a functions are called arguments. When using print, if multiple arguments are provided, separated by commas, print() will include all of them in a single display line. For example:
Notice that both strings and numbers can be used when providing multiple arguments to the print() function.
Another way to include multiple values when using the print() statement, is to
concatenate the values into a single string. Concatenate
means adding strings together to build a longer string. The is done by using the "+" operator. For example:
One important rule about concatenation: numbers cannot be concatenated as they are. Numbers must be converted to strings. This is accomplished using Python's str() function, which converts a number into a string. Look at the examples below:
The first statement results in an error, because a number cannot be concatenated with a string. (Using the "+" operator with two numbers just adds them; it does not concatenate them.)
A Better Way to print() with Multiple Values
While both of the methods above can be used to use print() with one or more arguments, both of those methods are a little clumsy. There is a much better way: an f-string. An f-string is not a function, but a way to provide and format one or more arguments in the print() function. An f-string is created by prefixing a string argument with "f". Values can then be included in the string using curly brackets, such as the following examples:
Within the f-string, each arguments can be a literal or variable, and a string or a number. Each argument is enclosed in curly-brackets. F-strings are an example of string interpolation: creating strings with placeholders that are populated with literal or variable values, something available in most modern languages.