¡GoConqr estrena diseño! Cuéntanos qué te parece.

Mapa mental

Python 3

Mike Peterson Computer Studies Not an exam Inglés

Contenido del mapa

Python 3

  1. Print

    • print("string")
    • print(variable)
      • print first letter = print(ListVariable[0])
        • print last letter = print(ListVariable[-1])
    • print(variable + "string") - called concatenate
      • print("Captain {} is {} years old.".format(name, age))
    • print("""Multi-line text""")
  2. Syntax

    • "string"
      • """Multi-line text"""
      • combined_string = ("Captain {} is {} years old.".format(name, age))
        • combined_string_with_var_to_two_decimal_places = ("This items costs: {:.2f}".format(float)
      • Example_string = 'this_is_a_string'
        • newList = Example_string.split('_')
          • newList = ['this', 'is', 'a', 'string']
            • newString = '-'.join(newList)
              • newString = 'this-is-a-string'
          • Note: to seperate by tab key '\t' or new line '\n'
        • Example_string.strip('_')
          • removes '_'
        • Example_string.replace('_', ' ')
          • replaces first arg with second
        • Example_string.find('i')
          • returns list with indices of arg
    • int(1)
    • float(0.2)
      • round(float, No. of decimals)
  3. def function(arg1, arg2):

    • return responseA, responseB
    • to set a default value: arg2=global_variable_name
  4. Control Flow

    • If (not) condition 1 and/or (not) condition 2:
      • elif (not) condition 3
        • else: (not) condition 4
    • try: function()
      • except Type-of-error:
        • print "You raised an error" (for example)
    • for temp_variable in list:
      • if temp_variable == a condition that means it's not wanted:
        • continue
      • if temp_variable == a condition which can stop the search
        • break
  5. Lists

    • list = [data]
      • list = [[sub-list], [sub-list2]]
    • listA_and_B = zip(listA, listB)
      • - Note this will create a zip object not a list
        • listA_and_B = list(ListA_and_B)
    • list.append(data)
      • list2 = list1 + [data1, data2]
      • list = [data1, data2, data3]
        • newList = [var for var in list if var (condition)]
    • len(list)
    • list[0] (first) list[-1] (last)
      • list[0:2] (first 3)
        • list[2:4] (third and fourth)
          • list[-2:] (last two items)
    • list.count("Character") - returns int
    • list.sort() - sorts original list, doesn't return anything
    • newList = sorted(list) - returns a new list
    • list = list[1:] - removes first datum
      • list = list[:-1] - removes last datum
  6. Tuples

    • my_tuple = (Data1, Data2, Data3)
      • Works like a list to store multiple data types
        • can print elements with my_tuple[x]
          • Can NOT overwrite data (inmutable)
      • Var1, Var2, Var3 = my_tuple
  7. PANDAS / CSV

    • import pandas as pd
      • import csv
        • Data is put in a dataframe
          • var = pd.read_csv('NameOfFile.csv')
            • var = pd.read_xlsx('NameOfFile.xlsx')
              • excel file
              • var = pd.read_csv('NameOfFile.txt')
                • USING THE DATA
                  • print(dfvar.head(number_of_rows))
                    • prints top rows
                  • print(dfvar.tail(number_of_rows))
                    • prints bottom rows
                  • print(dfvar.columns)
                    • prints a list type format
                    • print(dfvar.columns[i])
                      • prints column at index i
                    • number of columns = len(dfvar.columns)
                  • print all in column - print(dfvar[Name_of_column])
                    • multiple columns, use a list - print(dfvar[[column1, column2]]
                  • iloc = index location function
                    • print rows X to Y - print(dfvar.iloc[rowX: rowY)
                    • print specific data point X,Y - print(dfvar.iloc[X, Y])
                  • iterrows = iterate through rows
                    • i.e. for every index, return data in all rows
                    • for index, row in dfvar.iterrows():
                      • print(index, row)
                        • to only print data from certain columns;-
                          • print(index, row[column1: etc])
                  • iterating through index for data with conditions
                    • print rows that meed condition using .loc ;-
                      • print(dfvar.loc[dfvar['Column Name'] == 'Condition']
                  • dfvar.describe
                    • gives you information such as standard deviation & mean etc.
                  • Sorting
                    • dfvar.sort_values('Column_name')
                      • sorts alphabetically
                    • dfvar.sort_values(['Column 1', 'Column 2'], ascending = [1,0])
                      • ascending = 1 means true, i.e alphebetical or 0 - 100
                        • 0 = false i.e. reverse
                    • EDITING THE DATA
                      • Adding Columns
                        • dfvar['New_Column_Name'] = dfvar['Column1'] + dfvar['Column2']
                          • or
                            • dfvar['New_Column_Name'] = df.iloc[XColumn: YColumn, XRow:YRow].sum(axis = 1)
                              • note df.iloc[] locates the data within those ranges
                                • .sum(axis = 1) specifies adding them all together horizontally
                                  • axis = 0 would be vertically
                              • To move the position of the new column(s);-
                                • newList = list(dfvar.columns.values)
                                  • dfvar = dfvar[newList[X:Y] + [newList[-1]] + newList[Y+1:Z]]
                                    • The range X:Y is the columns you want before
                                      • newList[-1] is the final column that you have added
                                        • newList[Y+1:Z] is the remaining columns to go on the end
                                        • Note: newList[-1] is one column and will be considered a string, hence the extra square brackets
                      • Dropping a column
                        • dfvar = dfvar.drop(columns=['Column_name_to_be_removed']
                      • EXPORTING THE DATA
                        • dfvar.to_csv('filename.csv', index=False)
                          • Index=False prevents adding the index numbers as the left most column
                          • dfvar.to_excel('filename.xlsx', index=False)
                            • dfvar.to_csv('filename.txt', index=False, sep = '\t')
                              • sep='\t' means they will be seperated by a tab key
                • text file - note data is imported to single column
                  • to import to tab seperated;-
                    • var = pd.read_csv('NameOfFile.txt', delimiter=\t)
                      • where the '\t' denotes the tab key
            • comma seperated values file
          • length of data = len(var)
  8. Dictionairys

    • my_dict = {}
      • my_dict["new key"] = value
      • my_dict.update({key1: value1, key2: value2])
    • Combing two lists into a dictionairy
      • new_dict = {key:value for key, value in zip(list1, list2)}
  9. CLASSES

    • class ClassName:

Información

Descripción
Not an exam Computer Studies Mapa Mental sobre Python 3, creado por Mike Peterson el 10/05/2020.
Creado por
Mike Peterson
Visitas
4
Valoración

Escríbenos

¿Sobre qué es tu mensaje?

¿Qué te parece el nuevo diseño?

¿Te gusta?