Python

Data Types

Text Type:

str

Numeric Types:

int, float, complex

Sequence Types:

list, tuple, range

Mapping Type:

dict

Set Types:

set, frozenset

Boolean Type:

bool

Binary Types:

bytes, bytearray, memoryview

None Type:

NoneType

x = 5
# Check the typing and get the type
print(type(x), isInstance(x, str))

String

  • F-String was introduced in Python 3.6, which can be used to concatenate the string and variable

age = 36
txt = f"My name is John, I am {age}"
print(txt)

Logical

  • and : &&

  • or: ||

  • not: !

  • is vs ==

  • is is more strict than ==, as it also validate the object reference

Boolean

  • (), [], {}, "" , 0 , None , will equal to false

List

  • List Comprehension is similar with map method on javascript, having syntax sugar to create new array from a array

Tuple

  • A tuple is a collection which is ordered and unchangeable.

Set

  • A set is a collection which is unordered, unchangeable*, and unindexed

Dictionaries

  • Dictionaries which is similar with json object from javascript, containing key-value pair

Function

  • Here is the example of closure

  • In function , you can assign a default value to function argument, but need to take care of list, dict, as they are mutable and have unexpected behaviors

  • From above the result, since the default parameter is mutable, so the value is accumulated

  • Here is the fix below, by setting default parameter as None

Lambda

  • lambda function is a small anonymous function with a simple return value

Class

Last updated