Python
  • Useful websites
    • Coursera
    • edX
    • python.org
    • Codecademy
  • Useful links
  • General
  • 4 data types
    • list
    • set
    • tuple
    • dictionary
  • try / except
  • calculations:
  • x = 17
    y = 5
    x % y # remainder of the division (2)
    x // y # integer division (quotient) (3)
    print(divmod(x, y)) # returns a tuple with the quotient and the remainder (3, 2)
    # find the square root of 30
    # using 'math' (the most precise method)
    import math
    math.sqrt(30) # 5.477225575051661
    
    # using **
    30**0.5 # 5.477225575051661
    
    # using pow()
    pow(30, 0.5) # 5.477225575051661
  • escape characters:
  • x = 'I don\'t know' # \ is an escape character
    print(x) # I don't know
    x = 'first line\nsecond line' # \n is a newline character
    print(x) # first line
             # second line
    # to print a backslash, use r (raw string) as a prefix
    print(r'C:\Users\Owner\Desktop') # C:\Users\Owner\Desktop
    # raw strings cannot end in a backslash. To include a backslash at the end, use double backslashes
    print('C:\\Users\\Owner\\Desktop\\') # C:\Users\Owner\Desktop\
  • string operations:
  • Name = 'Doe' # string
    Age = 30 # integer
    Height = 5.9 # float
    is_adult = True # boolean
    print (f'{Name} is {Age} years old, {Height} feet tall and is an adult: {is_adult}') # Doe is 30 years old, 5.9 feet tall and is an adult: True
    # 'input() function calls for user input
    name = input("Enter your name: ")
    print("Hello, " + name + "!")

    If you type 'Jack' in the box and press ENTER, the result will display: Hello, Jack!

    # User inputs are strings. We must convert it to a number using a type conversion function.
    temperature = input("Enter the temperature in Fahrenheit: ")
    celsius = ((float(temperature) - 32) * 5/9)
    print("The temperature in Celsius is:", celsius)
    
    # open and read the file as nouvelles in VSC
      with open("C:/Users/Owner/Desktop/Transcripts.csv", "r", encoding="utf-8") as file: # "utf-8" specifies encoding for special characters
          nouvelles = file.read()
    
    # Alors que s'est ouverte samedi la Semaine de la francophonie, quelque 2,5 millions de personnes ne maîtrisent pas, en France, les compétences de base du français – parler, lire, écrire. À Paris, une association propose des activités de lecture et joue un rôle crucial dans l’évolution individuelle et professionnelle de ceux qui en bénéficient. Reportage.
    
    # count most frequenct 3 words in nouvelles in descending order
    words = nouvelles.split()
    word_count = {}
    for word in words:
        if word in word_count:
            word_count[word] += 1
        else:
            word_count[word] = 1
    word_count = sorted(word_count.items(), key=lambda x: x[1], reverse=True)
    print(word_count[:3]) # [('de', 5), ('la', 2), ('en', 2)]
  • Upload files to Google Colab:
  • from google.colab import files
    uploaded = files.upload()
    # click "Choose Files" and read the file
    with open('transcript.csv', 'r') as file:
             transcript = file.read()
    # ChatGPT's method to filter most common 100 words
    from google.colab import files
    uploaded = files.upload()
    
    with open('Transcripts.csv', 'r') as file:
             df = file.read()
    
    import pandas as pd
    from collections import Counter
    import re
    
    # Tokenize words (removing punctuation and converting to lowercase)
    words = re.findall(r'\b\w+\b', df.lower())
    
    # Count word frequencies
    word_counts = Counter(words)
    
    # Get the 100 most common words
    most_common_words = word_counts.most_common(100)
    
    # Convert to DataFrame for display
    common_words_df = pd.DataFrame(most_common_words, columns=['Word', 'Count'])
    
    # dowload the file to the computer
    common_words_df.to_csv('common_words.csv', index=False)
    files.download('common_words.csv')
    # find out non-integers
    x = [1, 5, 'hello']
    
    for i, item in enumerate(x):
      if not isinstance(item, int):
        print(f"{i}: {item}") # 2: hello