Первая программа

In [1]:
print('Привет, Python!')
Привет, Python!

Числовые типы

Интегральные типы

int

In [1]:
1
Out[1]:
1
In [2]:
1 + 2
Out[2]:
3
In [3]:
2 - 3
Out[3]:
-1
In [4]:
3 * 4
Out[4]:
12
In [5]:
4 / 5
Out[5]:
0.8
In [6]:
type(4 / 5)
Out[6]:
float
In [7]:
4 // 5
Out[7]:
0
In [8]:
5 ** 6
Out[8]:
15625
In [9]:
6 % 7
Out[9]:
6
In [10]:
a = 1
a += 1
a
Out[10]:
2
In [11]:
a++
  File "<ipython-input-11-bdceb1bc89e4>", line 1
    a++
       ^
SyntaxError: invalid syntax
In [12]:
++a
Out[12]:
2
In [13]:
a = int()
a
Out[13]:
0
In [14]:
2 ** 1024
Out[14]:
179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216

bool

In [1]:
True
Out[1]:
True
In [2]:
False
Out[2]:
False
In [3]:
not True
Out[3]:
False
In [4]:
True and False
Out[4]:
False
In [5]:
False or True
Out[5]:
True
In [6]:
1 == 2
Out[6]:
False
In [7]:
1 != 2
Out[7]:
True
In [8]:
1 is 1
Out[8]:
True

Вещественный тип

float

In [1]:
0.2
Out[1]:
0.2
In [2]:
1E-2
Out[2]:
0.01
In [3]:
float('+inf')
Out[3]:
inf

Комплексный тип

complex

In [1]:
2.3j + 4
Out[1]:
(4+2.3j)

Sequences

immutable

str

In [1]:
s = 'Привет, Python!'
In [2]:
"qwer"
Out[2]:
'qwer'
In [4]:
s[0]
Out[4]:
'П'
In [5]:
s[1]
Out[5]:
'р'
In [6]:
len(s)
Out[6]:
15
In [7]:
s[len(s) - 1]
Out[7]:
'!'
In [8]:
s[-1]
Out[8]:
'!'
In [9]:
s[-2]
Out[9]:
'n'
In [10]:
type(s[0])
Out[10]:
str
In [11]:
s[1:3]
Out[11]:
'ри'
In [12]:
s[:6]
Out[12]:
'Привет'
In [13]:
s[8:]
Out[13]:
'Python!'
In [14]:
s[8:-1]
Out[14]:
'Python'
In [15]:
s[::2]
Out[15]:
'Пие,Pto!'
In [16]:
s[::-1]
Out[16]:
'!nohtyP ,тевирП'
In [17]:
s + '!!!'
Out[17]:
'Привет, Python!!!!'
In [18]:
s * 10
Out[18]:
'Привет, Python!Привет, Python!Привет, Python!Привет, Python!Привет, Python!Привет, Python!Привет, Python!Привет, Python!Привет, Python!Привет, Python!'
In [19]:
'Py' in s
Out[19]:
True

s.<tab>

s.capitalize s.expandtabs s.isalpha s.isprintable s.lower s.rindex s.splitlines s.upper s.casefold s.find s.isdecimal s.isspace s.lstrip s.rjust s.startswith s.zfill s.center s.format s.isdigit s.istitle s.maketrans s.rpartition s.strip s.count s.format_map s.isidentifier s.isupper s.partition s.rsplit s.swapcase s.encode s.index s.islower s.join s.replace s.rstrip s.title s.endswith s.isalnum s.isnumeric s.ljust s.rfind s.split s.translate

s.find?

Type: builtin_function_or_method String form: <built-in method find of str object at 0x1108dd718> Docstring: S.find(sub[, start[, end]]) -> int

Return the lowest index in S where substring sub is found, such that sub is contained within S[start:end]. Optional arguments start and end are interpreted as in slice notation.

Return -1 on failure.</code>

In [20]:
s.find('привет')
Out[20]:
-1
In [21]:
s.lower().find('привет')
Out[21]:
0
In [22]:
for e in s:
    print(e)
П
р
и
в
е
т
,
 
P
y
t
h
o
n
!

bytes

In [1]:
b = b'abc'
In [2]:
b[0]
Out[2]:
97
In [3]:
b[1]
Out[3]:
98
In [4]:
b[2]
Out[4]:
99
In [5]:
b'текст'
  File "<ipython-input-5-102b35411efc>", line 1
    b'текст'
            ^
SyntaxError: bytes can only contain ASCII literal characters.
In [6]:
b = b'\x00\xff'
In [7]:
b[0]
Out[7]:
0
In [8]:
b[1]
Out[8]:
255
In [9]:
s = 'Привет, Python!'

s.encode?

Type: builtin_function_or_method String form: <built-in method encode of str object at 0x108bb68b8> Docstring: S.encode(encoding='utf-8', errors='strict') -> bytes

Encode S using the codec registered for encoding. Default encoding is 'utf-8'. errors may be given to set a different error handling scheme. Default is 'strict' meaning that encoding errors raise a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and 'xmlcharrefreplace' as well as any other name registered with codecs.register_error that can handle UnicodeEncodeErrors.</code>

In [10]:
s.encode()
Out[10]:
b'\xd0\x9f\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82, Python!'
In [11]:
s.encode('cp1251')
Out[11]:
b'\xcf\xf0\xe8\xe2\xe5\xf2, Python!'
In [12]:
b = s.encode('cp1251')
In [13]:
s2 = b.decode('cp1251')
In [14]:
s2
Out[14]:
'Привет, Python!'

tuple

In [1]:
t = 1, 'asdf', True, 0.1, 4j, b'abc'
t
Out[1]:
(1, 'asdf', True, 0.1, 4j, b'abc')
In [2]:
t[0]
Out[2]:
1
In [3]:
t[0]
Out[3]:
1
In [4]:
t[1]
Out[4]:
'asdf'
In [5]:
u = 1, t
u
Out[5]:
(1, (1, 'asdf', True, 0.1, 4j, b'abc'))
In [6]:
u[0]
Out[6]:
1
In [7]:
u[1]
Out[7]:
(1, 'asdf', True, 0.1, 4j, b'abc')
In [8]:
u[1][1]
Out[8]:
'asdf'
In [9]:
t = tuple()
t
Out[9]:
()
In [10]:
t = ()
t
Out[10]:
()
In [11]:
t = (1)
t
Out[11]:
1
In [12]:
t = 1,
t
Out[12]:
(1,)
In [13]:
print(1, 2, 3)
1 2 3
In [14]:
print((1, 2, 3))
(1, 2, 3)

mutable

list

In [1]:
a = [1, '2', 3]
a[0] = False
a
Out[1]:
[False, '2', 3]
In [2]:
a[1] = [3, 4, 5]
a
Out[2]:
[False, [3, 4, 5], 3]
In [3]:
a[1][0]
Out[3]:
3

a.<tab>

a.append a.clear a.copy a.count a.extend a.index a.insert a.pop a.remove a.reverse a.sort

bytearray

In [1]:
b = bytearray(b'abc')
b[0] = 100
b
Out[1]:
bytearray(b'dbc')

b.<tab>

b.append b.decode b.index b.isspace b.lstrip b.reverse b.rstrip b.title b.capitalize b.endswith b.insert b.istitle b.maketrans b.rfind b.split b.translate b.center b.expandtabs b.isalnum b.isupper b.partition b.rindex b.splitlines b.upper b.clear b.extend b.isalpha b.join b.pop b.rjust b.startswith b.zfill b.copy b.find b.isdigit b.ljust b.remove b.rpartition b.strip b.count b.fromhex b.islower b.lower b.replace b.rsplit b.swapcase

Sets

set

In [1]:
s = {1, 2, 1}
s
Out[1]:
{1, 2}
In [2]:
s = {1, True, 0.1, 2j, 'asdf', b'abc', (1, 2)}
s
Out[2]:
{(1, 2), 0.1, True, 2j, b'abc', 'asdf'}
In [3]:
s = {}
type(s)
Out[3]:
dict
In [4]:
s = set()
s
Out[4]:
set()
In [5]:
s = {[]}
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-889faf072b04> in <module>()
----> 1 s = {[]}

TypeError: unhashable type: 'list'
In [6]:
{set()}
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-9acc59e59f6f> in <module>()
----> 1 {set()}

TypeError: unhashable type: 'set'
In [7]:
s = {1, True, 0.1, 2j, 'asdf', b'abc', (1, 2)}

s.<tab>

s.add s.discard s.issuperset s.union s.clear s.intersection s.pop s.update s.copy s.intersection_update s.remove s.difference s.isdisjoint s.symmetric_difference s.difference_update s.issubset s.symmetric_difference_update

frozenset

In [1]:
fs = frozenset({1, 2, 3, 4})
fs
Out[1]:
frozenset({1, 2, 3, 4})
In [2]:
{fs}
Out[2]:
{frozenset({1, 2, 3, 4})}
In [3]:
for e in fs:
    print(e)
1
2
3
4

Dictionaries

dict

In [1]:
d = {}
d
Out[1]:
{}
In [2]:
d = {'one': 1, 'two': 2}
d['one']
Out[2]:
1
In [3]:
d['two'] = 10
d
Out[3]:
{'one': 1, 'two': 10}
In [4]:
for k in d:
    print(k, d[k])
one 1
two 10

d.<tab>

d.clear d.fromkeys d.items d.pop d.setdefault d.values d.copy d.get d.keys d.popitem d.update

In [5]:
d.items()
Out[5]:
dict_items([('one', 1), ('two', 10)])
In [6]:
d.keys()
Out[6]:
dict_keys(['one', 'two'])
In [7]:
d.values()
Out[7]:
dict_values([1, 10])
In [8]:
for k, v in d.items():
    print(k, v)
one 1
two 10
In [9]:
d = {1: []}
d
Out[9]:
{1: []}
In [10]:
d = {[]: 2}
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-10-2c64e5fa0136> in <module>()
----> 1 d = {[]: 2}

TypeError: unhashable type: 'list'
In [11]:
hash('asdf')
Out[11]:
2456217102423620831
In [12]:
hash([])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-12-6420c1e61a67> in <module>()
----> 1 hash([])

TypeError: unhashable type: 'list'

This website does not host notebooks, it only renders notebooks available on other websites.

Delivered by Fastly, Rendered by Rackspace

nbviewer GitHub repository.

nbviewer version: 67ee47e

nbconvert version: 5.3.1

Rendered (Tue, 20 Feb 2018 03:25:31 UTC)