Language Feature
Compared to Java Same:
- Everything is an object design and excellent cross-platform support
- Relatively standard libraries
- Run on virtual machines, python does this automatically at runtime
Difference:
- Python is strongly dynamically typed. Make it very easy to write and not hard to read.
- A lot machine learning resources
Variables
Built-in Atomic Data Types int
, float
, bool
.
Built-in Collection Data Types:
Ordered Collection:
list
,string
,tuple
Integers in Python3 are unbounded
- Unlike integers, floats are not infinite precision, and it’s convenient to refer to infinite as
float('inf')
andfloat('-inf')
- Random methods
random.randrange(28)
,random.randint(8,16)
,ranodm.random()
,random.shuffle(A)
,random.choice(A)
- Unordered Collection:
set
,dictionary
Exception Handler
1 | try: |
all
and any
all()
: checks if all the values in that iterable are non-False
any()
: checks if any of the values in that iterable are non-False
Usage:1
2
3
4
5
6
7
8
9"""
Check if list1 contains all elements in list2
"""
result = all(elem in list1 for elem in list2)
"""
Check if list1 contains any of the elements in list2
"""
result = any(elem in list1 for elem in list2)
Efficiency
list
in python is much slower thanset()
xrange
takes less space thanrange
, becausexrange
returns anobject
andrange
returns alist