2.2. Syntax Types¶
int
- Integer numberfloat
- Floating point numberbool
- Boolean valuestr
- Character stringNone
- Empty value (null)list
- Ordered sequence of objects (can be modified)tuple
- Ordered sequence of objects (cannot be modified)set
- Unordered sequence of objects (can be modified)dict
- Key-value data structuretype()
- Returns exact type of an argumentisinstance()
- Allows for checking if value is expected type
This concept is only briefly described here. More information will be in upcoming chapters.
2.2.1. Basic types¶
>>> data = 1 # int
>>> data = 1.2 # float
>>> data = True # bool
>>> data = False # bool
>>> data = None # NoneType
>>> data = 'abc' # str
2.2.2. Sequences¶
>>> data = [1, 2, 3] # list
>>> data = (1, 2, 3) # tuple
>>> data = {1, 2, 3} # set
2.2.3. Mappings¶
>>> data = {'a': 1, 'b': 2} # dict
2.2.4. Type Checking¶
type()
- Returns type of an argumentisinstance()
- Allows for checking if value is expected typeTo check if type is what you expected use
type()
orisinstance()
Later you will learn the difference
>>> x = 1
>>> type(x)
<class 'int'>
>>> x = 1
>>> type(x) is int
True
>>> x = 1
>>> isinstance(x, int)
True
2.2.5. Use Case - 0x01¶
>>> name = 'Mark Watney'
>>> age = 42
>>> height = 178.0
>>> is_astronaut = True
>>> friends = None
2.2.6. Assignments¶
"""
* Assignment: Syntax Types Int
* Type: class assignment
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min
English:
1. Define variable `result` with value 1
2. Run doctests - all must succeed
Polish:
1. Zdefiniuj zmienną `result` z wartością 1
2. Uruchom doctesty - wszystkie muszą się powieść
Tests:
>>> import sys; sys.tracebacklimit = 0
>>> from pprint import pprint
>>> assert result is not Ellipsis, \
'Assign your result to variable `result`'
>>> assert type(result) is int, \
'Variable `result` has invalid type, should be int'
>>> assert result == 1, \
'Variable `result` has invalid value, should be 1'
>>> pprint(result)
1
"""
# with value 1
# type: int
result = ...
"""
* Assignment: Syntax Types Float
* Type: class assignment
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min
English:
1. Define variable `result` with value 1.2
2. Run doctests - all must succeed
Polish:
1. Zdefiniuj zmienną `result` z wartością 1.2
2. Uruchom doctesty - wszystkie muszą się powieść
Tests:
>>> import sys; sys.tracebacklimit = 0
>>> from pprint import pprint
>>> assert result is not Ellipsis, \
'Assign your result to variable `result`'
>>> assert type(result) is float, \
'Variable `result` has invalid type, should be float'
>>> assert result == 1.2, \
'Variable `result` has invalid value, should be 1.2'
>>> pprint(result)
1.2
"""
# with value 1.2
# type: float
result = ...
"""
* Assignment: Syntax Types Str
* Type: class assignment
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min
English:
1. Define `result` with text 'Hello World'
2. Run doctests - all must succeed
Polish:
1. Zdefiniuj zmienną `result` z tekstem 'Hello World'
2. Uruchom doctesty - wszystkie muszą się powieść
Hints:
* Either quotes (") or apostrophes (') will work
Tests:
>>> import sys; sys.tracebacklimit = 0
>>> from pprint import pprint
>>> assert result is not Ellipsis, \
'Assign your result to variable `result`'
>>> assert type(result) is str, \
'Variable `result` has invalid type, should be str'
>>> assert result == 'Hello World', \
'Variable `result` has invalid value, should be "Hello World"'
>>> pprint(result)
'Hello World'
"""
# with Hello World
# type: str
result = ...
"""
* Assignment: Syntax Types Bool
* Type: class assignment
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min
English:
1. Define variable `result` with value True
2. Run doctests - all must succeed
Polish:
1. Zdefiniuj zmienną `result` z wartością True
2. Uruchom doctesty - wszystkie muszą się powieść
Hint:
* Note that True is capitalized
Tests:
>>> import sys; sys.tracebacklimit = 0
>>> from pprint import pprint
>>> assert result is not Ellipsis, \
'Assign your result to variable `result`'
>>> assert type(result) is bool, \
'Variable `result` has invalid type, should be bool'
>>> assert result is True, \
'Variable `result` has invalid value, should be True'
>>> pprint(result)
True
"""
# with value True
# type: bool
result = ...