Key Takeaways: Print
Key Takeaways: Print
Print(“Hello world”)
“”-is a denotation to be used in order to type a string, command which says to the computer that the
text between “” is not a code but rather take it as is or the computer will read and output it as is without
any command lines hidden, any texts written between “” will be taken literally/ output as is. Not as a
code but as a data
To execute a function we need a function call or as we call it function invocation or invoke a function,, to
call it in order to be executed>>>function_name(argument)- that is what program structure should look
like in order to invoke a function or to make the computer familiarize it in its own computer
programming library to be known and to be executed
Strings, numbers, characters, logical values, objects - any of these may be successfully passed
to print().
A line can be empty (i.e., it may contain no instruction at all) but it must not contain two, three or
more instructions. This is strictly prohibited.
\- it is an escape character,,it does not have meaning by itself rather it suggest a new different meaning
after the backslash>>print(“ukilam lubuajen/nukilam”)>>output should be:
ukilam lubuajen
ukilam
so the backslash introduces a new different kind of meaning which is n which when introduced by
backslash makes or adds a new meaning that form a special symbol named a newline
a keyword argument consists of three elements: a keyword identifying the argument(ex: end), then the
equal sign (=), and last but not the list is the value assigned to that argument
Key takeaways
1. The print() function is a built-in function. It prints/outputs a specified message to the
screen/consol window.
2. Built-in functions, contrary to user-defined functions, are always available and don't have to be
imported. Python 3.8 comes with 69 built-in functions. You can find their full list provided in
alphabetical order in the Python Standard Library.
3. To call a function (this process is known as function invocation or function call), you need to
use the function name followed by parentheses. You can pass arguments into a function by placing
them inside the parentheses. You must separate arguments with a comma, e.g., print("Hello,",
"world!") . An "empty" print() function outputs an empty line to the screen.
4. Python strings are delimited with quotes, e.g., "I am a string" (double quotes), or 'I am a
string, too' (single quotes).
6. In Python strings the backslash ( \ ) is a special character which announces that the next
character has a different meaning, e.g., \n (the newline character) starts a new output line.
7. Positional arguments are the ones whose meaning is dictated by their position, e.g., the second
argument is outputted after the first, the third is outputted after the second, etc.
8. Keyword arguments are the ones whose meaning is not dictated by their location, but by a
special word (keyword) used to identify them.
9. The end and sep parameters can be used for formatting the output of the print() function.
The sep parameter specifies the separator between the outputted arguments (e.g., print("H",
"E", "L", "L", "O", sep="-") , whereas the end parameter specifies what to print at the end
of the print statement.
The characteristic of the numeric value which determines its kind, range, and application, is called
the type.
you can write this number either like this: 11111111, or like that: 11_111_111.
There are two additional conventions in Python that are unknown to the world of mathematics. The
first allows us to use numbers in an octal representation.
If an integer number is preceded by an 0O or 0o prefix (zero-o), it will be treated as an octal value.
This means that the number must contain digits taken from the [0..7] range only.
300000000 .
To avoid writing out so many zeros, physics textbooks use an abbreviated form, which you have
probably already seen: 3 x 108 .
In Python, the same effect is achieved in a slightly different way - take a look:
3E8
Strings
Strings are used when you need to process text (like names of all kinds, addresses, novels, etc.), not numbers.
You already know a bit about them, e.g., that strings need quotes the way floats need points.
However, there is a catch. The catch is how to encode a quote inside a string which is already delimited by
quotes.
The first is based on the concept we already know of the escape character, which you should remember is
played by the backslash. The backslash can escape quotes too. A quote preceded by a backslash changes its
meaning - it's not a delimiter, but just a quote. This will work as intended:
The second solution may be a bit surprising. Python can use an apostrophe instead of a quote. Either of these
characters may delimit strings, but you must be consistent.
If you open a string with a quote, you have to close it with a quote.
If you start a string with an apostrophe, you have to end it with an apostrophe.
Boolean values
To conclude with Python's literals, there are two additional ones.
They're not as obvious as any of the previous ones, as they're used to represent a very abstract value
- truthfulness.
Each time you ask Python if one number is greater than another, the question results in the creation of some
specific data - a Boolean value.
The name comes from George Boole (1815-1864), the author of the fundamental work, The Laws of Thought,
which contains the definition of Boolean algebra - a part of algebra which makes use of only two distinct
values: True and False , denoted as 1 and 0 .
A programmer writes a program, and the program asks questions. Python executes the program, and provides
the answers. The program must be able to react according to the received answers.
You'll never get a response like: I don't know or Probably yes, but I don't know for sure.
True
False
You cannot change anything - you have to take these symbols as they are, including case-sensitivity.
Run the code in the Sandbox to check. Can you explain the result?
+ , - , * , / , // , % , **
A ** (double asterisk) sign is an exponentiation (power) operator. Its left argument is the base, its
right, the exponent.
Classical mathematics prefers notation with superscripts, just like this: 23. Pure text editors don't
accept that, so Python uses ** instead, e.g., 2 ** 3 .
Look at the code below and try to predict the results once again:
print (-6 // 4)
print (6. // -4)
Note: some of the values are negative. This will obviously affect the result. But how?
The result is two negative twos. The real (not rounded) result is -1.5 in both cases. However, the
results are the subjects of rounding. The rounding goes toward the lesser integer value, and the
lesser integer value is -2 , hence: -2 and -2.0 .
Its graphical representation in Python is the % (percent) sign, which may look a bit confusing.
print(12 % 4.5)
3.0 - not 3 but 3.0 (the rule still works: 12 // 4.5 gives 2.0 ; 2.0 * 4.5 gives 9.0 ; 12 -
9.0 gives 3.0 )
List of priorities
Since you're new to Python operators, we don't want to present the complete list of operator
priorities right now.
Instead, we'll show you its truncated form, and we'll expand it consistently as we introduce new
operators.
Priority Operator
1 + , - unary
2 **
3 * , / , // , %
4 + , - binary
Note to self: % can be used for limiting the maximum numbers in the output process
Note: we've enumerated the operators in order from the highest (1) to the lowest (4) priorities.
Key takeaways
1. An expression is a combination of values (or variables, operators, calls to functions - you will
learn about them soon) which evaluates to a value, e.g., 1 + 2 .
2. Operators are special symbols or keywords which are able to operate on the values and perform
(mathematical) operations, e.g., the * operator multiplies two values: x * y .
You already know that you can do some arithmetic operations with these numbers: add, subtract, etc. You'll be
doing that many times.
But it's quite a normal question to ask how to store the results of these operations, in order to use them in
other operations, and so on.
How do you save the intermediate results, and use them again to produce subsequent ones?
Python will help you with that. It offers special "boxes" (containers) for that purpose, and these boxes are
called variables - the name itself suggests that the content of these containers can be varied in (almost) any
way.
a name;
a value (the content of the container)
Variables do not appear in a program automatically. As developer, you must decide how many and which
variables to use in your programs.
You must also name them.
If you want to give a name to a variable, you must follow some strict rules:
the name of the variable must be composed of upper-case or lower-case letters, digits, and the
character _ (underscore)
the name of the variable must begin with a letter;
the underscore character is a letter;
upper- and lower-case letters are treated as different (a little differently than in the real world
- Alice and ALICE are the same first names, but in Python they are two different variable names, and
consequently, two different variables);
the name of the variable must not be any of Python's reserved words (the keywords - we'll explain
more about this soon).
Keywords
Take a look at the list of words that play a very special role in every Python program.
They are called keywords or (more precisely) reserved keywords. They are reserved because you
mustn't use them as names: neither for your variables, nor functions, nor any other named entities
you want to create.
The meaning of the reserved word is predefined, and mustn't be changed in any way.
Fortunately, due to the fact that Python is case-sensitive, you can modify any of these words by
changing the case of any letter, thus creating a new word, which is not reserved anymore.
import
You mustn't have a variable named in such a way - it is prohibited. But you can do this instead:
Import
These words might be a mystery to you now, but you'll soon learn the meaning of them.
Using variables
You're allowed to use as many variable declarations as you need to achieve your goal, like this:
var = 1
account_balance = 1000.0
print (var)
You're not allowed to use a variable which doesn't exist (in other words, a variable that was not assigned a
value).
var = 1
print(Var)
We've tried to use a variable named Var , which doesn't have any value (note: var and Var are different
entities, and have nothing in common as far as Python's concerned).
REMEMBER
You can use the print() function and combine text and variables using the + operator to output strings and
variables, e.g.:
var = "3.8.5"
The input() function
We're now going to introduce you to a completely new function, which seems to be a mirror reflection of the
good old print() function.
The function is named input() . The name of the function says everything.
The input() function is able to read data entered by the user and to return the same data to the running
program.
The program can manipulate the data, making the code truly interactive.
Virtually all programs read and process data. A program which doesn't get a user's input is a deaf program.
Note:
The program prompts the user to input some data from the console (most likely using a keyboard,
although it is also possible to input data using voice or image);
the input() function is invoked without arguments (this is the simplest way of using the function);
the function will switch the console to input mode; you'll see a blinking cursor, and you'll be able to
input some keystrokes, finishing off by hitting the Enter key; all the inputted data will be sent to your
program through the function's result;
note: you need to assign the result to a variable; this is crucial - missing out this step will cause the
entered data to be lost;
then we use the print() function to output the data we get, with some additional remarks.
Try to run the code and let the function show you what
We've said it already, but it must be unambiguously stated once again: the result of
the input() function is a string.
A string containing all the characters the user enters from the keyboard. It is not an integer or a float.
This means that you mustn't use it as an argument of any arithmetic operation, e.g., you can't
use this data to square it, divide it by anything, or divide anything by it.
The * (asterisk) sign, when applied to a string and number (or a number and string, as it remains
commutative in this position) becomes a replication operator:
string * number
number * string
It replicates the string the same number of times specified by the number.
For example:
Type conversion: str()
You already know how to use the int() and float() functions to convert a string into a number.
This type of conversion is not a one-way street. You can also convert a number into a string,
which is way easier and safer - this operation is always possible.
str (number)
To be honest, it can do much more than just transform numbers into strings, but that can wait for
later.
The "right-angle triangle" again
Here is our "right-angle triangle" program again:
We've modified it a bit to show you how the str() function works. Thanks to this, we can pass the
whole result to the print() function as one string, forgetting about the commas.
hour = hour + mins // 60 # find a number of hours hidden in minutes and update the hour
Key takeaways
1. The print() function sends data to the console, while the input() function gets data from the
console.
2. The input() function comes with an optional parameter: the prompt string. It allows you to write a
message before the user input, e.g.:
name = input("Enter your name: ")
3. When the input() function is called, the program's flow is stopped, the prompt symbol keeps blinking (it
prompts the user to take action when the console is switched to input mode) until the user has entered an input
and/or pressed the Enter key.
NOTE
You can test the functionality of the input() function in its full scope locally on your machine. For resource
optimization reasons, we have limited the maximum program execution time in Edube to a few seconds. Go to
Sandbox, copy-paste the above snippet, run the program, and do nothing - just wait a few seconds to see what
happens. Your program should be stopped automatically after a short moment. Now open IDLE, and run the
same program there - can you see the difference?
Tip: the above-mentioned feature of the input() function can be used to prompt the user to end a program.
Look at the code below:
input ()
3. The result of the input() function is a string. You can add strings to each other using the concatenation
( + ) operator. Check out this code:
Boolean Values, Conditional Execution, Loops, Lists and List Processing, Logical and
Bitwise Operations