Py Modules Flashcards
How to import a module?
> > Import
Name 4 useful modules?
collections ( counter ), random, math, webbrowser
How can a random integer be created?
]] from random import seed ]] from random import random ]] seed( ) ]] print( random(), random() ) ]]0.7215400323407826 0.22876222127045265
– the function random will consistently generate the same random numbers every time
https://machinelearningmastery.com/how-to-generate-random-numbers-in-python/
Dict, what’s the difference between a dictionary and a list?
Dictionary store key, value pairs, whereas lists are a range of values
How do you find out if an object is a dictionary or not?
.type( )
How would you express a dictionary’s structure?
{ key:value, key:value }
Do you know of any rules for dictionary’s keys?
Immutable, can’t be a list for the key but can be used for the value, not ordered
How to add a single new value to a dictionary?
]] [ ‘key’ ] = ‘value’
]] t1_dict[ ‘abc’ ] = 99
How to see all pairs in a dictionary?
]] < dict_name >
or
]] < dict_name >.items( )
1) How to see all keys in dictionary?
2) How to see all values for a dictionary?
]] .keys( )
]] .values( ) `
Value in Dict or List, how to check if a specific value is in a dict or a list?
– ERROR: needs to be updated –
]] if in
– recturns True or False
– WRONG –
Value in Dict, how to fetch a specific value from a Dict?
]] .get( ‘key’ )
- returns pair’s value
- changed from True or False
how to compare two textstrings similiarity?
]] import difflib
]] difflib.get_close_matches( ‘word_to_compare’, [ ‘words’, ‘‘in, ‘‘list, ‘to’, ‘compare’, ‘against’], 1, 0.6 )
- 1: how many words to return as a close match
- 2: ratio to be fulfilled for the words to return
how to find the ratio of similarity between two textstrings?
]] difflib.SequenceMatcher( None, ‘word_comp_1’, ‘word_comp_2’).ratio()
how to see all installed modules?
]] help( ‘modules’ )