Python Built-in Data Types

Casting

#Integers

x = int(1)   # x will be 1
y = int(2.8) # y will be 2
z = int("3") # z will be 3

#Floats

x = float(1)     # x will be 1.0
y = float(2.8)   # y will be 2.8
z = float("3")   # z will be 3.0
w = float("4.2") # w will be 4.2

#Strings

x = str("s1") # x will be 's1'
y = str(2)    # y will be '2'
z = str(3.0)  # z will be '3.0'

Dictionary

>>> empty_dict = {}
>>> a = {"one": 1, "two": 2, "three": 3}
>>> a["one"]
1
>>> a.keys()
dict_keys(['one', 'two', 'three'])
>>> a.values()
dict_values([1, 2, 3])
>>> a.update({"four": 4})
>>> a.keys()
dict_keys(['one', 'two', 'three', 'four'])
>>> a['four']
4

Key: Value pair, JSON like object

Set

set1 = {"a", "b", "c"}   
set2 = set(("a", "b", "c"))

Set of unique items/objects

Tuple

my_tuple = (1, 2, 3)
my_tuple = tuple((1, 2, 3))

Similar to List but immutable

Booleans

my_bool = True 
my_bool = False
bool(0)     # => False
bool(1)     # => True

Numbers

x = 1    # int
y = 2.8  # float
z = 1j   # complex
>>> print(type(x))
<class 'int'>

Strings

hello = "Hello World"
hello = 'Hello World'
multi_string = """Multiline Strings
Lorem ipsum dolor sit amet,
consectetur adipiscing elit """

See: Strings

Comments