Show Menu
Cheatography

Python Fundamental Week 1 Cheat Sheet by

Syntax related to Python Fundamental (Week 1)

Python Basic & Data Structure

VARIABLE ASSIGNMENT & DATA TYPE DETECTION
create a variable
full = 1
data type detection
print (type(­var­iab­le_­name))
PYTHON LIST
create a list
my_list = ["my­", "­lis­t", 0, 1 ]
getting elements in a list
x = ["a", "­b", "­c", "­d"]
x[1]
x[-3] # same result!
slicing & dicing
my_lis­t[s­tar­t:end]
manipu­lating a list (update)
fam = ["li­z", 1.73, "­emm­a",2]
fam[3] = 1.86
manipu­lating a list (remove)
del(fa­m[2])  # Remove "­emm­a" 
manipu­lating a list (add element)
x = ["a", "­b", "­c", "­d"]
y = x + ["e", "­f"]
PYTHON SET
create a set
myset = {"ap­ple­", "­ban­ana­", "­che­rry­"}
PYTHON TUPLE
create a tuple
mytuple = ("ap­ple­", "­ban­ana­", "­che­rry­")
getting elements in a tuple
mytuple[1]
slicing & dicing in a tuple
mytupl­e[s­tar­t:end]

Python Loop

For: lặp đúng bằng số lần của tập hợp, danh sách
fruits = ["ap­ple­", "­ban­ana­", "­che­rry­"­,"st­raw­ber­ry"]
for i in fruits:
print(i)
While: cho đến khi condition = True
i = 0
while i < 6: 
    i= i + 2
print(i)

Python if else condition

Phân biệt Python if elif và (multiple) if condition
 

Data Structure (Dicti­onary)

PYTHON DICTIONARY
create dictionary
d1 = {1: 'Geeks', 2: 'For', 3: 'Geeks'} # create dictionary using { }
k = ["Fr­uit­s", "­Veg­eta­ble­s", "­Dri­nks­"]
val = [["A­ppl­e", "­Ban­ana­"], ["Ca­rro­t", "­Spi­nac­h"], ["Wa­ter­", "­Jui­ce"]]
d = dict(z­ip(k, val))  print(d) # create a dictionary of lists using zip
get list of values, keys
dictio­nar­y_n­ame.va­lues()
dictio­nar­y_n­ame.keys()
access specific value of key
dictio­nar­y_n­ame­['s­pec­ifi­c_key']
dictionary manipu­lation (add key & value)
 dictio­nar­y_n­ame­['n­ew_­key'] = 'new_v­alue' 
dictionary update / remove key
 dictio­nar­y_n­ame­['k­ey_­to_­upd­ate'] = 'value­_to­_up­date'
del dictio­nar­y_n­ame­['k­ey_­to_­rem­ove']

Data Structure (String)

PYTHON STRING
create a string
 s = "­GfG­" 
access character in string
 s[1]
string slicing
s[start : end]
common string methods (len of string)
s = "­Gee­ksf­orG­eek­s"
print(­len(s))
# output: 13
common string methods (upper, lower)
 s = "­Hello World" 
 print(­s.u­pper())   # output: HELLO WORLD
 print(­s.l­ower())   # output: hello world
common string methods (strip)
s = "   Gfg   "
print(­s.s­trip()) # remove space from s
# output: Gfg
another string methods
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

            Python 3 Cheat Sheet by Finxter

          More Cheat Sheets by hangvtk7777

          draft Cheat Sheet
          Google Sheet Course Note Cheat Sheet
          hangvtk_python Cheat Sheet