Learn Python In One Day Flashcards

1
Q
What do these operators do in Python:
// and **
A
// is floor division
** is exponentiation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

In Java we have printf. What are two equivalents in Python?

A

“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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

There are three functions that provide for type casting. What are they?

A

int(), float() and str()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Declare a list called userAge.

A

userAge = [10,, 15, 21, 8, 9]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Declare an empty list called userAge. Then add 3 items to it.

A
Note that append() adds to the end of a list
userAge = []
userAge.append(10)
userAge.append(13)
userAge.append(19)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Write code to access the
first element of a list
the second from last element of a list
elements 2 to 4 inclusive

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you delete an item from a list?

A

del mylist[index]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What’s the main difference between a tuple and a list?

A

The tuple is immutable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Declare a dictionary with 2 elements.

A

userAge - {“James”:10, “Peter”:12}
You can also use
userAge - dict(James:10, Peter:12)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly