Python Flashcards
download file from web from command line
import urllib
urllib.urlretrieve (“http://www.example.com/songs/mp3.mp3”, “mp3.mp3”)
import urllib
urllib.urlretrieve (“http://www.example.com/songs/mp3.mp3”, “mp3.mp3”)
move and rename a file in os
import os
os.rename(‘somefile’, ‘somewhere/else’)
return a new sorted list in ascending order
sorted([5, 2, 3, 1, 4])
Use list.sort() method of a list to modify list in place.
> > > a = [5, 2, 3, 1, 4]
a.sort()
a
[1, 2, 3, 4, 5]
Add item to the end of a list
list.append(x)
Return the number of times x appears in a list.
list.count(x)
Argument specifier for any object with a string representation.
%s
Argument specifier for integers
%d
Argument specifier for floating point numbers
%f
Floating point numbers with a fixed amount of digits to the right of the .
%.f
Argument specifier for Integers in hex representation (lowercase/uppercase)
%x/%X
print the number of a’s in a string called string
print string.count(‘a’)
print a slice of the string string
print string[3:7]
print the length of a string called string
print len(string)
print the location of the first occurrence of the letter b in a string called string
pring string.index(‘b’)
make letters in var astring uppercase
astring.upper( )
make letters in a string called astring lowercase
astring.lower( )
argument in a function
The piece of code you put between the function’s parenthesis when you call it
parameter in function
Name you put between the function’s parenthesis when you define it.
Function import aka Pulling in just a single function from a module
from module import function (where module and function are the names of the module and the function)
Import everything in a module without having to call that module every time you want to use it in the future.
from module import *
explicit string conversion
str( )
Syntax for a string formatter (to print strings without explicitly typing the variable
print “%s” % (variable)
Order of operations in boolean
Not, and, or