Python syntax

Cards (42)

  • type(x) used to determine the data type
  • """ """ multiple line comment
  • arr = [1,2,3] list
  • arr = [1,2,3,4,5,]
    print(arr[2:4])
    [3,4]
  • len(arr) length
  • arr.append(1) add item to the end of the list
  • arr.remove(1) remove the value in a list
  • arr.pop() remove the last element in a list
  • del arr[0] delete using index in a list
  • arr.clear() clears the list
  • list3 = list1 + list2 join two lists (1st method)
  • for i in list2:
    list1.append(i)

    joint two lists(2nd method)
  • list1.extend(list2) join two lists(3rd method)
  • arr = (1, 2, 3, 4, 5) tuple
  • arr = {1, 2, 3,} set
  • arr = {'a': 1, 'b': 2, 'c': 3} dictionary
  • tuple = (1,4,3)
    list = list(tuple)
    list[1] = 2
    tuple = tuple(list)

    changing tuple values
  • set3 = set1.union(set2) join two sets (1st method)
  • set1.update(set2) join two sets(2nd method)
  • pip install numpy to install numpy
  • import numpy as np to import numpy library
  • import array as arr to import array library
  • create an 'int' array with a size of 2 using numpy method
    arr = np.empty(2,dtype=int)
  • creating array using numpy method (syntax)
    arr = np.empty(shape,dtype=datatype,order='C')
  • create 'int' array with elements [1,2,3]
    arr = arr.array('i',[1,2,3])
  • create int 3x3 array using numpy method
    arr = np.empty([3,3],dtype=int)
  • declare a series (series as variable)with 1,2,3 values and a,b,c as index
    series = pd.Series([1,2,3],index=['a','b','c'])
  • dict = {'a':1,'b':2,'c':3}
    create a series using this dictionary (use series as variable)
    series = pd.Series(dict)
  • dict = {'a':1,'b':2,'c':3}
    create series only key 'a' and 'c' only (use series as variable)
    series = pd.Series(dict,index=['a','c'])
  • dict = {'a':1,'b':2,'c':3,'d':4,'e':5}series = pd.Series(dict)
    print only the key 'a' to 'c' using slicing
    print(series['a':'c'])
  • csvfile.csv
    read this csv file (use data as variable)
    data = pd.read_csv("csvfile.csv")
  • create a dictionary ('name' 1.mike 2.irish 3.mari) ('gender' 1.male 2.female 3.female) then,
    create a dataframe using that dictionary (use data as variable)
    dict = {'name':['mike','irish','mari'],'gender':['male',female','female'])
    data = pd.DataFrame(dict)
  • create a 2x2 numpy array ('a','b')(1,2) (use arr as variable)
    create a dictionary with a key ('letter' and 'number') with the values of the numpy array (use dict as variable)
    create a dataframe using that dictionary (use data as variable)
    arr = np.array([['a','b'],[1,2])
    dict = {'letter':arr[0],'number':arr[1])
    data = pd.DataFrame(dict)
  • "data" transposing dataframe or csv file data (use data_trans as variable to hold the "data")
    data_trans = data.transpose()
  • head() method returns the headers and specified number of row, starting from the top. One of the most used method for getting a quick overview of the dataframe.
  • tail() method for viewing the last rows of the dataframe. tail() method returns the headers and specified numbers of rows, starting from the bottom.
  • df.info() gives you more information about the dataset.
  • arr = np.arange(1,10+1)
    [1,2,3,4,5,6,7,8,9,10]
  • arr = np.arange(8)
    make a 2x4 array using .reshape()
    arr = np.arange(8).reshape(2,4)
  • arr = np.arange(8).reshape(4,2)
    [[0,1],
    [2,3],
    [4,5],
    [6,7]]