Python :) Flashcards

1
Q

If/else if/else structure in python

A
if condition : 
     #statements
elif condition : 
     #statements 
else:
     #statements
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

For loop in python

A
for identifier in range (int,int): 
    #statements 

This is non inclusive (eg in range (0,5) will only run where the identifier is 0,1,2,3 and 4)

Can also be used like

for identifier in range (int,int,increment): 
    #statements 

Also non inclusive for the second int

for identifier in StringExp:
   # statements 

Starts counting from 0, and ends on 1 less than the number of letters in the word

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

Userinput in python

A

Identifier = input(input prompt)

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

Modulus in python

A

%

Eg 5%2 would return 1

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

Integer division in python

A
//
Eg 5//2 evaluates to 2
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Output in python

A

Print (Exp)

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

Random number in python

A

from random import rand range

randrange( IntExp, IntExp)

The second IntExp is exclusive

Eg.

from random import randrange
num1 = randrange(3, 7)
# generates an integer between 3 and 6
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Less than in python

A

Exp < Exp

Eg.
4 <6
“A” < “B”
“adam” < “adele”

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

Greater than (pyth)

A

Exp > Exp

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

Equal to (pyth)

A

Exp == Exp

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

Not equal to (pyth)

A

Exp != Exp

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

Less than or equal to (pyth)

A

Exp <= Exp

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

Greater than or equal to

A

Exp >= Exp

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

Relational operators can be used on (pyth)

A

Numbers strings and characters

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

Logical AND (pyth)

A

BoolExp and BoolExp

Eg,

(3 == 3) and (3 <= 4)

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

Logical OR (pyth)

A

BoolExp or BoolExp

17
Q

Logical NOT

A

not BoolExp

18
Q

While loop (pyth)

A
while BoolExp:
   # indented statements here
19
Q

Repeat until loop (pyth)

A
# statements here while BoolExp:
   # copy of statements here (indented)
20
Q

Length of array

A

len(Identifier)

21
Q

For loop for the length of an array

A
for Identifier in ListExp:
# indented statements here
22
Q

Defining Subroutine in python

A
def Identifier(parameters): 
   # indented statements here
23
Q

Subroutine return value

A

return Exp

24
Q

Calling subroutines

A

Identifier(parameters)

25
Q

String length

A

len(StringExp)

26
Q

Position of a character

A

StringExp.find(CharExp)

27
Q

Substring of a string

A

StringExp[IntExp:IntExp]
Last IntExp is exclusive

Starts counting from 0

28
Q

Accessing a single character in a string

A

StringExp[IntExp]

Starts counting from 0

29
Q

String concatenation

A

StringExp + StringExp

30
Q

Convert string to integer

A

int(StringExp)

31
Q

Convert string to real

A

float(StringExp)

32
Q

Convert integer to string

A

str(IntExp)

33
Q

Convert real to string

A

str(RealExp)

34
Q

Converting character to character code

A

ord(CharExp)

35
Q

Converting character code to character

A

chr(IntExp)

36
Q

Input in python

A

input(StringExp)

StringExp is the prompt

37
Q

Output in python

A

print(Exp, …,Exp)

Eg
print (“a”, “g”) will output
a g

print("Mary had ", end="")
print("a little lamb")
# will print the text on the same line due to end=“”
38
Q

Formatted output

A

f”…{identifier}…{identifier}…”

Eg.

name = "BT"
staff = 125000
print(f"{name} has {staff} staff")
# outputs "BT has 125000 staff"