Basics Flashcards

1
Q

Variables

A

Do not need to be declared. Assigned by:

a=10
b=20

or

a,b=10,20

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

‘a’ + ‘a’

A

aa

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

‘a’ * 3

A

aaa

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

Length of strength ‘abc’?

A

len(‘abc’)

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

Get the letter ‘a’ from the string ‘abc’

A

‘abc’[0]

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

Get the letter ‘c’ from the string ‘abc’

A

‘abc’[2] or ‘abc’[-1]

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

From ‘hello world’, get the substring ‘world’

A

‘hello world’[6:]

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

From ‘hello world’, get the substring ‘ell’

A

‘hello world’[1:3]

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

Concatenate the strings ‘hello’ and ‘world’ together.

A

‘hello’ + ‘world’

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

If-else if-else statement

A

if(x

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

Sequence of numbers from 1 to 10

A

range(1,10)

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

Sequence of odd numbers from 1 to 10

A

range(1,10,2)

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

For loop

A

for i in range(1,10):

print i

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

While loop

A

while(x<10):
print x
x=x+1

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

Sequence of numbers from 0 to 100000000000

A

xrange(0, 100000000000)

xrange generates values only as they are needed, so it is faster than range for a large sequence of numbers

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

Delete a variable a

17
Q

Create a function called someFunction

A
def someFunction():
    print 'I am a function'
18
Q

Default function values

A
def someFunction(x=true):
    if(x=true):
        print 'default value'
    else:
        print 'changed value'
19
Q

Passing parameters of a function in any order

A

power(x=4, num=2)

power(num=2, x=4)

20
Q

Variable number of parameters in a function

A
def multi_add(*args):
    result=0
    for x in args:
        result = result + x
    return result
print multi_add(2,2,3,3,10)
print multi_add(2,2)
21
Q

Conditional statements

A

Lets you combine if-else statements into a single line.

x,y=1,2
print “x is less than y” if(x

22
Q

Get the index and the value when enumerating over an array in a for loop.

A

days = [“M”,”T”,”W”,”R”]
for i,d in enumerate(days):
print i,d

# 0 M
# 1 T
# 2 W
# 3 R
23
Q

How you import modules into python?

A
#import the date class from the datetime module
from datetime import date
#import everything in the module
import calendar
24
Q

Get the current date and time.

A

datetime. now()
date. today()
datetime. time(datetime.now())

25
Q

Get the date and time a year from now.

A

from datetime import date
from datetime import time
from datetime import datetime
from datetime import timedelta

datetime.now() + timedelta(days=365)

26
Q

Get the date and time a week ago.

A

from datetime import date
from datetime import time
from datetime import datetime
from datetime import timedelta

datetime.now() - timedelta(weeks=1)