This article is a very brief but succinct pressing of everything that a novice developer or QA engineer should know about Python. I hope that the diligence attached when writing this article will help many prepare for interviews for relevant vacancies and expand their IT outlook.
The article should not be taken as a textbook, but as a convenient cheat sheet or “reference signal” (as my history teacher called such “creativity” in school). There will be no detailed definitions, explanations in the whole chapter, but only clear terms, lists, brief pressing of a code. The article is based on the book “Learning Python (5th edition)” by Mark Lutz, so you may not worry about its correctness and accuracy. So, let's begin.
Python in brief
Python is a high-level, general-purpose programming language (often called a scripting language), focused on increasing developer productivity and code readability.
Python benefits:
')
- Software quality
- High development speed
- Program portability
- Support Libraries
- Integration of components (you can call functions from C / C ++ libraries)
Python main features:
- Dynamic typing (while it is strict)
- Automatic memory management
- Modular programming
- Built-in object types
- Utility libraries
The process of starting programs:
- The script is compiled (program translation) into byte-code (platform-independent representation of the source code of the program, .pyc file)
- The byte code is transferred to the PVM virtual machine.
Comparison with C / C ++:
- No build phase
- Byte code is not a binary machine code (can not be executed as quickly)
Alternative Python implementations:
- CPython (implementation on ANSI C)
- Jython (implementation on Java classes)
- IronPython (implementation for use with .Net)
Object Types in Python
Data in Python is represented in the form of objects — either embedded, or created using Python constructions or other (for example, C).
Programs <= Modules <= Instructions <= Expressions
Forms of displaying objects:
- repr (as a program code, for example, 6.2830000000044)
- str (more readable, for example 6.283)
A string is an immutable sequence (ordered collection) of other objects (single-character strings) in the Python language.
Some operations on sequences:
- len (s) - length
- s [0] - access to the element
- s [-1] - access to the element from the end of the sequence
- s [1: 3] - cut from offset 1 to 2 (not 3)
- s [:] - copy all content (not to be confused with a = s)
- s * 8 - repeat
- s + 'xyz' - concatenation
Special tab characters:
- \ n - end of line
- \ t - tab
- ord ('\ n') - byte with a numeric value
- 'A \ 0B \ 0C' - \ 0 binary zero
[Hint] To display all available methods on a variable: dir (s)
Lists (lists) are collections of objects of arbitrary types ordered by location, the size of which is unlimited.
Lists are sequences => support all operations on sequences. The only difference is that the result of such operations are lists, not strings.
Benefits of listings:
- Ability to create nested lists
- Using list generator expressions
col2 = [row[1] for row in M]
Dictionaries (dictionary) are not sequences, they are collections of objects, where they are accessed not by certain offsets from the beginning of the collection, but by keys. Changeable type. Nesting is also possible.
D = {'food': 'Spam', 'quantity': 4, 'color': 'pink'}
or
D = {} D['name'] = 'Bob'
A tuple is a list that cannot be changed. Tuples are sequences, like lists, but they are immutable, like strings. Possible nesting. It is possible to simultaneously store objects of different types.
T = (1, 2, 3, 4)
File objects are the main interface between Python programming code and external files on a computer. Files are one of the basic types, but they represent something unusual, since for files there is no possibility of creating objects in the form of literals. Instead, to create a file object, you must call the open function, passing it the name of the external file and the mode string of the file access.
f = open('data.txt', 'w')
The best way to read files today is not to read its entire contents — files provide iterators that provide automatic line-by-line reading of the file contents in for loops and in other contexts.
Sets are unordered collections of unique and immutable objects. Sets are created with the built-in set function.
X = set('spam')