Python overview
To get you grasp of an idea about Python it's better to just fly over it awhole. To see main syntax nuances, basic operations and workflow. We won't stop to explain everything in detail as it will be covered in next sessions. Now it's just a show-off.
So, in next few slides we are going to see:
Basic types
Basic operators and methods
Conditions
Functions
OOP
Exceptions
Basic types
ℹ️
Type | Name | Short description | Mutable | Example |
---|---|---|---|---|
| None | "Empty" value | No |
|
| Boolean | Boolean value ( | No |
|
| Integer | Integer numbers | No |
|
| Float | Floating point number | No |
|
| String | Textual data - sequense of characters | No |
|
| List | Mutable sequense of any kind of objects | Yes |
|
| Tuple | Immutable sequense of objects | No |
|
| Set | Mutable collection of unique objects | Yes |
|
| Frozen Set | Immutable collection of unique objects | No |
|
| Dictionary | The collection of key-value pairs | Yes |
|
Immutable types (cannot be changed after creation)
str
int
float
tuple
Mutable types (can be changed)
list
set
dict
Basic operators:
Assignment operators:
=
,+=
,*=
,-=
,/=
Comparison operators:
==
,!=
,>
,<
,<=
,>=
is
Logical operators:
and
,or
,not
Arithmetic operators:
Basic operators, sequences:
String operators:
List operators:
Conditions, cycles
Conditions
Cycles
Importing
Work with Files
Functions
Operator "def" is used for creating functions
🪄 Code:
📟 Output:
Lambda function - short (functional) method of defining a function. Used for sorting, building jump tables, when the function is not planned to be used after some operation and with map
/filter
.
🪄 Code:
📟 Output:
🪄 Code:
📟 Output:
Misc.
Swapping two variables
🪄 Code:
📟 Output:
Read keyboard input
Passing argument to function
There is a common question - how exactly arguments are passing in Python's functions. Usually there are two cases: value and pointer. By here we don't have either of those - because everything in python is wrapped in Python objects.
So, argument passing to a function is done not by value (we don't have "raw" data in Python, only objects that wrapping it) and not by reference (reference is int number of memory location). This is done by object-reference. In other words we are passing object itself (it's almost like passing the pointer to an object in memory).
🪄 Code:
📟 Output:
Decorators
🪄 Code:
📟 Output:
OOP
In Python, everything is an object, even classes. Classes are object too! More of that, classes as objects have their own classes which called metaclasses. Multiple inheritance and mixins are supported.
The language supports extensive introspection of types and classes. Types can be read and compared—types are instances of type. The attributes of an object can be extracted as a dictionary.
Operators can be overloaded in Python by defining special member functions—for instance, defining add on a class permits one to use the + operator on members of that class.
🪄 Code:
📟 Output:
Exceptions
Sometime Python encounters the error during the work of some code or function - it creates an event called exception. If this exception is not handled (the program doesn't expect it) Python stops stops the program execution.
For example the file with the following code will print the message about Traceback and never the line "You won't see me!":
🪄 Code:
📟 Output:
But we can handle this error, doing so tells Python that the occurrence of this error is expected and we don't need to stop the program.
Handling exceptions is done via try
/except
/finally
blocks:
🪄 Code:
📟 Output:
Last updated