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’)