Python Essentials

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Python Essentials 1:

Module 2
Data types, variables, basic input-output operations, basic operators

The word print that you can see here is a function name.

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

1. What is the effect the  print()  function causes?

The effect is very useful and very spectacular. The function:

 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.

2. What arguments does  print()  expect?

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() .

3. What value does the  print()  function return?

None. Its effect is enough.

The print() function - instructions
Python requires that there cannot be more than one instruction in a line.

empty print()produce a newline in the output console


The print() function - the escape and newline characters
Interestingly, while you can see two characters, Python sees one.

\n.

The backslash ( \ ) has a very special meaning when used inside strings - this is called the
escape character.

The letter n placed after the backslash comes from the word newline.

Both the backslash and the n form a special symbol named a newline character, which urges
the console to start a new output line.

The print() function - using multiple arguments

The arguments are separated by commas.

Two conclusions emerge from this example:

 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.

The print() function - the positional way of passing the


arguments
The way in which we are passing the arguments into the print() function is the most common
in Python, and is called the positional way

The print() function - the keyword arguments


The print() function has two keyword arguments that you can use for your purposes. The first
of them is named end.

In order to use it, it is necessary to know some rules:

 a keyword argument consists of three elements: a keyword identifying the argument


( end  here); an equal sign ( = ); and a value assigned to that argument;
 any keyword arguments have to be put after the last positional argument (this is very
important)
As the  end  argument has been set to nothing, the  print()  function outputs nothing too, once its
positional arguments have been exhausted.

The console should now be showing the following text:

My name is Monty Python.

Note: no newlines have been sent to the output.

We've said previously that the  print()  function separates its outputted arguments with spaces.
This behavior can be changed, too.

The keyword argument that can do this is named  sep  (like separator).

he  sep  argument delivers the following results:

My-name-is-Monty-Python.

output

The  print()  function now uses a dash, instead of a space, to separate the outputted
arguments.

Note: the  sep  argument's value may be an empty string, too.

Both keyword arguments may be mixed in one invocation

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).

5. Computer programs are collections of instructions. An instruction is a command to perform a


specific task when executed, e.g., to print a certain message to the screen.
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.

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.

Look at the table below:

Priority Operator

1 + ,  - unary

2 **

3 * ,  / ,  // ,  %

4 + ,  - binary

2.4.1.1 Variables - data-shaped boxes

Keywords
Take a look at the list of words that play a very special role in every Python program.

['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class',


'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for',
'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal',
'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
Key takeaways

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)

 3.1.1.5 Making decisions in Python


 3.7.1.1 Lists in advanced applications

You might also like