Learn Python In One Day Flashcards
What do these operators do in Python: // and **
// is floor division ** is exponentiation
In Java we have printf. What are two equivalents in Python?
“string”%(identifiers) The syntax is the same as in Java
“string {position:the printf formatting} more string {position:more formatting}”(item for 1st position, item for second position) and so on
e.g. “my name is {0:s} and I am {1:d} years old”(‘Sukhdev’, 44)
If you don’t indicate the position of the arguments the compiler will just take them in order.
There are three functions that provide for type casting. What are they?
int(), float() and str()
Declare a list called userAge.
userAge = [10,, 15, 21, 8, 9]
Declare an empty list called userAge. Then add 3 items to it.
Note that append() adds to the end of a list userAge = [] userAge.append(10) userAge.append(13) userAge.append(19)
Write code to access the
first element of a list
the second from last element of a list
elements 2 to 4 inclusive
myList[0]
myList[-2]
myList[1:4] //Remember the last number is excluding that element and because the list is zero based, element 2 is [1].
How do you delete an item from a list?
del mylist[index]
What’s the main difference between a tuple and a list?
The tuple is immutable
Declare a dictionary with 2 elements.
userAge - {“James”:10, “Peter”:12}
You can also use
userAge - dict(James:10, Peter:12)