Python 2-05-2021 Workshop
Python 2-05-2021 Workshop
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
1. Literals are notations for representing some fixed values in code. Python has various types of literals -
for example, a literal can be a number (numeric literals, e.g., 123), or a string (string literals, e.g., "I am a
literal.").
2. The binary system is a system of numbers that employs 2 as the base. Therefore, a binary number is
made up of 0s and 1s only, e.g., 1010 is 10 in decimal.
Octal and hexadecimal numeration systems, similarly, employ 8 and 16 as their bases respectively. The
hexadecimal system uses the decimal numbers and six extra letters.
3. Integers (or simply ints) are one of the numerical types supported by Python. They are numbers
written without a fractional component, e.g., 256, or -1 (negative integers).
4. Floating-point numbers (or simply floats) are another one of the numerical types supported by
Python. They are numbers that contain (or are able to contain) a fractional component, e.g., 1.27.
5. To encode an apostrophe or a quote inside a string you can either use the escape character, e.g., 'I\'m
happy.', or open and close the string using an opposite set of symbols to the ones you wish to encode,
e.g., "I'm happy." to encode an apostrophe, and 'He said "Python", not "typhoon"' to encode a (double)
quote.
6. Boolean values are the two constant objects True and False used to represent truth values (in numeric
contexts 1 is True, while 0 is False.
EXTRA
There is one more, special literal that is used in Python: the None literal. This literal is a so-called
NoneType object, and it is used to represent the absence of a value. We'll tell you more about it soon.
Remember: Data and operators when connected together form expressions. The simplest expression is
a literal itself.
1. A variable is a named location reserved to store values in the memory. A variable is created or
initialized automatically when you assign a value to it for the first time. (2.1.4.1)
2. Each variable must have a unique name - an identifier. A legal identifier name must be a non-empty
sequence of characters, must begin with the underscore(_), or a letter, and it cannot be a Python
keyword. The first character may be followed by underscores, letters, and digits. Identifiers in Python
are case-sensitive. (2.1.4.1)
3. Python is a dynamically-typed language, which means you don't need to declare variables in it.
(2.1.4.3) To assign values to variables, you can use a simple assignment operator in the form of the equal
(=) sign, i.e., var = 1.
4. You can also use compound assignment operators (shortcut operators) to modify values assigned to
variables, e.g., var += 1, or var /= 5 * 2. (2.1.4.8)
5. You can assign new values to already existing variables using the assignment operator or one of the
compound operators, e.g.: (2.1.4.5)
var = 2
print(var)
var = 3
print(var)
var += 1
print(var)
6. You can combine text and variables using the + operator, and use the print() function to output strings
and variables, e.g.: (2.1.4.4)
var = "007"
print("Agent " + var)
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.:
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()
print("THE END.")
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:
Exercise 1
print(x * "5")
Check
Exercise 2
print(type(x))
Check
Prev Next
s=int((mins+dura)/60)
m=((mins+dura)%60)
if hour == 0:
hour=12
res=hour+s
print(res)
if res>24:
res= res%24
print(s)
print(res,m)