⬆️ ⬇️

Python-crib. Part 1 - Language and Object Types

image


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:

')



Python main features:





The process of starting programs:





Comparison with C / C ++:





Alternative Python implementations:





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:





A string is an immutable sequence (ordered collection) of other objects (single-character strings) in the Python language.



Some operations on sequences:





Special tab characters:





[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:





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') #  2.6  3.0     Y = {'h', 'a', 'm'} #  3.0     X, Y ({'a', 'p', 's', 'm'}, {'a', 'h', 'm'}) X & Y #  {'a', 'm'} X | Y #  {'a', 'p', 's', 'h', 'm'} X – Y #  {'p', 's'} 

Source: https://habr.com/ru/post/312674/



All Articles