Master Python. Ounce 1. Data Types.
I continue my undertaking. This article is a logical continuation of the first . It was nice to read your comments. I was hoping that this series of articles would be useful to someone, but I did not expect at all that there would be a large number of people interested. This makes the matter more serious and responsible.
Without further ado, right to the point.
As mentioned earlier, almost everything that we deal with when programming in python is objects. Object types can be either built-in or described by the programmer using classes. This article will focus on the built-in types of objects. They are:
- The numbers
Numbers, Strings, and Tuples
The reason that these types were consolidated into one section lies in one very important point. Without an understanding of which difficult errors can be made. The fact is that these types, unlike lists and dictionaries, are immutable objects. Those. having created them once, it is not possible to change their meaning. A fair question arises: how then do the following instructions work? The instruction x = 2 creates a numerical object with the value 2 and assigns a reference to this object to the variable x. Next, the instruction x = 3 creates a new numerical object with the value 3 and assigns a reference to it with the variable x. And an object with a value of 2 is deleted from memory by an automatic garbage collector. the last link to this object was lost. Thanks to this, the following is true:
>>> x = 2
>>> x = 3
>>> x = 'string'
>>> x = y = 3 # переменные x и y содержат одну и ту же ссылку на объект 3
>>> x = 2 # переменная x теперь содержит ссылку на другой объект 2
>>> y # y по-прежнему ссылается на объект 3
The topic will mention the is operator. This operator returns True when variables to the left and right of it contain pointers to the same object. The question arises about the strange behavior of this operator: Here it is the caching internal mechanisms of the interpreter itself. To optimize execution speed when declaring a numeric or short string object, a link and value pair is created in the cache. And later, when declaring the same object, a new object is not created, but the already created one is used. The conclusion suggests itself that the use of the is operator is untenable for numeric and string data types. Then everything is simple! Types of numeric literals:
>>> x = 2
>>> y = 2 # создано два разных объекта
>>> x is y # по идее должно быть возвращено значение False
- integers of unlimited length - the interpreter will determine the necessity of using objects of unlimited length and make the necessary transformations. There is no longer any need to monitor overflows.
- Floating point numbers
- octal and hexadecimal numbers
- complex numbers
Some operations on numbers:
>>> print 2 ** 150 # 2 в степени 150
>>> print int(5.89),round(5.89),round(5.89,1) # округление вниз, вверх, с точностью до 1 знака
5 6.0 5.9
>>> import math
>>> print math.sin(math.pi/2)
>>> import random
>>> random.randint(1,100) # случайное целое число в диапазоне от 1 до 100
Strings in apostrophes and in quotation marks are one and the same.
Strings in triple quotes - in triple quotes you can enclose a whole block of strings, for example, a whole HTML or XML document.
Unformatted lines - lines of the form r "\ n \ t" - sequences in such a line will not be replaced by special ones. Symbols
Unicode character strings - u'string '# loses relevance in version 3.0 more about changes 3.0
Basic operations on strings:
>>> print str (len (' string ')) * 3 # len () - returns the number of characters in line
666 # * - an overloaded operator that repeats the string the specified number of times
>>> 'r' in 'string' # the in operator returns True if the substring is found in the
>>> 'string'.replace (' r ',' ') # replacing one substring with another
' sting '
>>>' string'.find ('ri') # returns the offset number of the desired substring
>>> 'a , b, c'.split (',') # function of splitting into substrings by the specified character or substring
['a', 'b', 'c']
More complete information about string methods can be obtained by entering the help command in interactive mode (str).
The same lines have already been described by my future co-author of this series of articles here
Indexing, fetching, and slicing data sequences.
I think that it is possible to access the elements of a string by their indices, as in an array - you don’t need to talk too long. A slice is a rather powerful tool for selecting data from sequences, which is used not only in rows but also in collections, which will be discussed in this article a little later. General view: S [i: j: h], where S is the string, i is the offset value, the left border (inclusive), j is the offset value, the right border (it does not enter the slice), h is the step value. Examples: >>> 'string' [0: 4] 'stri' >>> 'string' [: - 2] 'stri' >>> 'string' [0: 5: 2] 'srn' >>> ' string '[0: 5: -2] ' nrs'
>>> 'string'[-2] # второй элемент с конца строки
Speaking of strings, of course, one cannot forget about regular expressions. In Python, to use them, you need to import the re module.
I hope that we can talk about them separately.
A tuple is an ordered, immutable collection of objects of arbitrary types that supports an arbitrary number of nesting levels.
The basics of syntax were also described by cleg in the article.
Lists and Dictionaries
Lists and dictionaries are mutable built-in data types. And this means, if we go back to where we started the conversation, that if you create a list Y, then make an assignment X = Y, and then change X, then Y will also change. This must be closely monitored! To prevent this from happening, you need to create a new object equivalent to the original one, for example, like this: X = Y [:]
Illustration: Or, if you want to ensure the immunity of the created object, use tuples instead of lists. Lists are an amazingly flexible tool, which is an ordered collection of elements that can include any objects with any level of nesting.
>>> M = L = range(2,12,3)
>>> print M,L
[2, 5, 8, 11] [2, 5, 8, 11]
>>> L = 112
>>> print M,L
[2, 5, 112, 11] [2, 5, 112, 11]
>>> M = L[:]
>>> L = 0
>>> print M,L
[2, 5, 112, 11] [2, 5, 0, 11]
Dictionary - is an unordered collection of objects, access to the elements of which is provided not with an offset, but with a key.
For the basics of syntax, you can also go to the clega article .
Here I will only make some additions and notes on the use of dictionaries:
- operations on sequences are not applicable to dictionaries since dictionaries are not a sequence, but a mapping.
- Keys do not have to be strings. The keys can be any immutable objects. Those. if we consider the built-in data types, then these are numbers, strings and tuples. It is clear that when using numbers, the dictionary turns into a semblance of a list, however, it remains in this case disordered. Using tuples as a key, it becomes possible to use compound keys. Instances of custom classes can also act as keys, provided that they implement certain methods that tell the interpreter that the object is immutable.
Many are containers for other objects. Sets are created by the built-in set function and support typical mathematical operations on sets . Any immutable objects can become elements of a set. After we have done certain operations on sets - they can be placed back in the list, for example, like this: That's all for today. Using the terminology of Radio-T, I want to ask a question :) Where do you tighten the degree of geekiness, in your opinion? If up, then in my opinion, loops and branches should be disclosed only in terms of the features inherent in Python. Thanks for attention! We continue :)
>>> M = set('habrahabr')
>>> L = set(['h','a','h','a'])
>>> M, L
(set(['a', 'h', 'r', 'b']), set(['a', 'h']))
>>> M & L
>>> M | L
set(['a', 'r', 'h', 'b'])
>>> M - L
>>> bool(M - L)
>>> pusto = set()
>>> S = 
>>> for i in M - L: