Python Essentials
Python Essentials
Python Essentials
Module 2
Data types, variables, basic input-output operations, basic operators
A function (in this context) is a separate part of the computer code able to:
cause some effect (e.g., send text to the terminal, create a file, draw an image, play a
sound, etc.); this is something completely unheard of in the world of mathematics;
evaluate a value (e.g., the square root of a value or the length of a given text) and return
it as the function's result; this is what makes Python functions the relatives of
mathematical concepts
takes its arguments (it may accept more than one argument and may also accept less
than one argument)
converts them into human-readable form if needed (as you may suspect, strings don't
require this action, as the string is already readable)
and sends the resulting data to the output device (usually the console); in other
words, anything you put into the print() function will appear on your screen.
Any. We'll show you soon that print() is able to operate with virtually all types of data offered
by Python. Strings, numbers, characters, logical values, objects - any of these may be
successfully passed to print() .
The print() function - instructions
Python requires that there cannot be more than one instruction in a line.
\n.
The backslash ( \ ) has a very special meaning when used inside strings - this is called the
escape character.
Both the backslash and the n form a special symbol named a newline character, which urges
the console to start a new output line.
a print() function invoked with more than one argument outputs them all on one line;
the print() function puts a space between the outputted arguments on its own
initiative.
We've said previously that the print() function separates its outputted arguments with spaces.
This behavior can be changed, too.
My-name-is-Monty-Python.
output
The print() function now uses a dash, instead of a space, to separate the outputted
arguments.
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).
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.
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
Keywords
Take a look at the list of words that play a very special role in every Python program.
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)
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"