Python Basic & Data Structure
VARIABLE ASSIGNMENT & DATA TYPE DETECTION |
create a variable |
|
data type detection |
print (type(variable_name))
|
PYTHON LIST |
create a list |
my_list = ["my", "list", 0, 1 ]
|
getting elements in a list |
x = ["a", "b", "c", "d"]
|
|
|
slicing & dicing |
|
manipulating a list (update) |
fam = ["liz", 1.73, "emma",2]
|
|
manipulating a list (remove) |
del(fam[2]) # Remove "emma"
|
manipulating a list (add element) |
x = ["a", "b", "c", "d"]
|
|
PYTHON SET |
create a set |
myset = {"apple", "banana", "cherry"}
|
PYTHON TUPLE |
create a tuple |
mytuple = ("apple", "banana", "cherry")
|
getting elements in a tuple |
|
slicing & dicing in a tuple |
|
Python Loop
For: lặp đúng bằng số lần của tập hợp, danh sách |
fruits = ["apple", "banana", "cherry","strawberry"]
|
|
|
While: cho đến khi condition = True |
|
|
|
|
Python if else condition
Phân biệt Python if elif và (multiple) if condition |
|
|
Data Structure (Dictionary)
PYTHON DICTIONARY |
create dictionary |
d1 = {1: 'Geeks', 2: 'For', 3: 'Geeks'} # create dictionary using { }
|
k = ["Fruits", "Vegetables", "Drinks"]
|
val = [["Apple", "Banana"], ["Carrot", "Spinach"], ["Water", "Juice"]]
|
d = dict(zip(k, val)) print(d) # create a dictionary of lists using zip
|
get list of values, keys |
dictionary_name.values()
|
dictionary_name.keys()
|
access specific value of key |
dictionary_name['specific_key']
|
dictionary manipulation (add key & value) |
dictionary_name['new_key'] = 'new_value'
|
dictionary update / remove key |
dictionary_name['key_to_update'] = 'value_to_update'
|
del dictionary_name['key_to_remove']
|
Data Structure (String)
PYTHON STRING |
create a string |
|
access character in string |
|
string slicing |
|
common string methods (len of string) |
|
|
# output: 13 |
common string methods (upper, lower) |
|
print(s.upper()) # output: HELLO WORLD
|
print(s.lower()) # output: hello world
|
common string methods (strip) |
|
print(s.strip()) # remove space from s
|
# output: Gfg |
another string methods |
|
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets
More Cheat Sheets by hangvtk7777