Chapter 4
Chapter 4
Chapter 4
1
ITBS103
❑ It's time to start writing some real, working Python code. It'll be very simple for the time being.
2
ITBS103
2
Your very first program
❑ Function invocation
❑ The function name (print in this case) along with the parentheses and argument(s), forms the function invocation.
Let's see:
➢ First, Python checks if the name specified is legal (it browses its internal data in order to find an existing function of the
name; if this search fails, Python aborts the code)
➢ second, Python checks if the function's requirements for the number of arguments allows you to invoke the function in this
way (e.g., if a specific function demands exactly two arguments, any invocation delivering only one argument will be
considered erroneous, and will abort the code's execution)
➢ third, Python leaves your code for a moment and jumps into the function you want to invoke; of course, it takes your
argument(s) too and passes it/them to the function;
➢ fourth, the function executes its code, causes the desired effect (if any), evaluates the desired result(s) (if any) and finishes its
task;
➢ finally, Python returns to your code (to the place just after the invocation) and resumes its execution.
3
ITBS103
❑ Examples
❑ As you can see, the empty print() invocation is not as empty as you may have expected ‒ it
does output an empty line, or (this interpretation is also correct) it outputs a newline.
❑ This is not the only way to produce a newline in the output console. We're now going to
4
show you another way.
ITBS103
4
Escape Characters
5
ITBS103
Escape Characters
❑ The print() function has two keyword arguments that you can use for your purposes. The first
is called end.
❑ As you can see, the end keyword argument determines the characters the print() function
sends to the output once it reaches the end of its positional arguments.
❑ The default behavior reflects the situation where the end keyword argument is implicitly used
in the following way: end="\n". 6
ITBS103
6
7
ITBS103
• Positional arguments
• This name comes from the fact that the meaning of the argument is dictated by its position
(e.g., the second argument will be outputted after the first, not the other way round).
• Keyword arguments
• Python offers another mechanism for the passing of arguments, which can be helpful when
you want to convince the print() function to change its behavior a bit.
• The mechanism is called keyword arguments. The name stems from the fact that the
meaning of these arguments is taken not from its location (position) but from the special
word (keyword) used to identify them.
8
ITBS103
8
The print() function has two keyword arguments that you can use for your purposes. The first
is called end.
print("Monty Python.")
any keyword arguments have to be put after the last positional argument (this is very
important)
In our example, we have made use of the end keyword argument, and set it to a string
containing one space. 9
ITBS103
The keyword argument that can do this is named sep (as in separator).
print("My", "name", "is", "Monty", "Python.", sep="-")
Output:
My-name-is-Monty-Python.
10
ITBS103
10
SECTION SUMMARY
11
ITBS103
11
SECTION SUMMARY
12
ITBS103
12
Understanding Literals in Python
▪ Literals are a way of representing data types in Python. They can be anything from numbers to textual data,
Boolean values, etc. Let’s discuss the various types of literals
13
ITBS103
13
14
ITBS103
14
Understanding Literals in Python
❑ Numeric Literals
▪ These literals contain only immutable numeric values. By immutable, it implies that one
cannot changes its values during runtime.
▪ Integers
▪ Float
▪ Complex
15
ITBS103
15
16
ITBS103
16
Understanding Literals in Python
17
ITBS103
17
18
ITBS103
18
Understanding Literals in Python
19
ITBS103
19
20
ITBS103
20
Understanding Literals in Python
21
ITBS103
21
22
ITBS103
22
Understanding Literals in Python
Boolean Values
In programming you often need to know if an expression
is True ro False.
You can evaluate any expression in Python, and get one of two
answers, True ro False.
print(10 > 9)
print(10 == 9)
print(10 < 9)
23
ITBS103
23
24
ITBS103
24
• Try it:
• bool(False)
bool(None)
bool(0)
bool("")
bool(())
bool([])
bool({}) 25
ITBS103
25
Variables
❑ Variables are containers for storing data values.
❑ Creating Variables
▪ Python has no command for declaring a variable.
▪ A variable is created the moment you first assign a value to it.
Variables do not need to be declared with any particular type, and
can even change type after they have been set.
Casting
If you want to specify the data type of a variable, this can be done
with casting.
26
ITBS103
26
Operators - data manipulation tools
❑ Basic operators
▪ An operator is a symbol of the programming language, which is able to operate on
the values.
27
ITBS103
27
28
ITBS103
28
Operators and their priorities
❑ List of priorities
29
ITBS103
29
print(9 % 6 % 2)
print(2 ** 2 ** 3)
30
ITBS103
30
SECTION SUMMARY
1) An expression is a combination of values (or variables, operators, calls to functions ‒ you will
learn about them soon) which evaluates to a certain 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.
4) A unary operator is an operator with only one operand, e.g., -1, or +3.
31
ITBS103
31
SECTION SUMMARY
➢ then the unary + and - (note: a unary operator to the right of the exponentiation
operator binds more strongly, for example 4 ** -1 equals 0.25)
➢ then: *, /, and %,
➢ and finally, the lowest priority: binary + and -.
32
ITBS103
32
Variables – data-shaped boxes
33
ITBS103
33
Variable names
❑ 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).
34
ITBS103
34
❑ Note that the same restrictions apply to function names.
❑ Python does not impose restrictions on the length of variable names, but that doesn't mean
that a long variable name is always better than a short one.
35
ITBS103
35
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.
36
ITBS103
36
37
ITBS103
37
38
ITBS103
38
Keywords
❑ A variable comes into existence as a result of assigning a value to it. Unlike in other languages, you don't
need to declare it in any special way.
39
Shortcut operators
40
ITBS103
40
Comments
❑ Creating a Comment
➢ Comments starts with a #, and Python will ignore them:
❑ Multiline Comments
41
ITBS103
41
42
ITBS103
42
❑ 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.
❑ Take a look at our example:
43
ITBS103
43
Note:
❑ the input() function is invoked with one argument ‒ it's a string containing a message;
❑ the message will be displayed on the console before the user is given an opportunity to
enter anything;
❑ input() will then do its job.
This variant of the input() invocation simplifies the code and makes it clearer.
44
ITBS103
44
❑ The last line of the sentence explains everything ‒ you tried to apply the ** operator
to 'str' (string) accompanied with 'float'.
❑ This is prohibited.
❑ This should be obvious - can you predict the value of "to be or not to be" raised to the power
of 2?
❑ We can't. Python can't, either.
❑ Have we fallen into a deadlock? Is there a solution to this issue? Of course there is.
45
ITBS103
45
46
ITBS103
46
Type casting (type conversions)
47
ITBS103
47
String operators
48
ITBS103
48
Type conversions once again
49
ITBS103
49