Python Flashcards

(52 cards)

1
Q

What is a string?

A

1 or more characters enclosed in speech marks

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

How do u write a print statement?

A

print(“…”)

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

What does concatenate mean?

A

Combining multiple strings into a single string

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

How to concatenate?

A

Using +
Eg. print(“…”+”…”)
U can also put variables in there, not just strings
Eg. print(“….”+ … + “…”)

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

What is an integer?

A

A whole number, called int

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

What is a floating point/real?

A

A number with a decimal, called float

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

What is mod?

A

Returns the remainder using the % symbol
Eg. 17%6=5

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

What is div?

A

Gives the integer number without rounding
does not show the remainders or decimals
Uses the symbol //
Eg.17//6=2

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

How to round numbers?
Eg. Round 17/6 to 3 decimal places

A

Use round(… , the amount of decimal places)
Eg.result = 17/6
round(result, 3)

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

How to say less than or equal to?

A

<=

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

How to say greater than or equal to?

A

> =

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

How to say not equal?

A

!=

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

How to say equal to?

A

==

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

What’s the difference between = and ==

A

= is for assigning a value
== is for comparing the 2 values either side

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

What is a variable?

A

A memory location that stored values which can change during execution
Eg. name=“Diya”

It can not start with a number, nor can it have a space

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

How to use , ?

A

It is used to separate separate strings, integers, real numbers and variables

Just remember to only add “ “ when u are talking about strings and not for variables or numbers
Eg. cost = 15
print (“The cost is £”, cost)

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

Can u use both + and , to concatenate?

A

Yes
But remember + can not be used as a separator and join a string and number so u have to turn numbers into strings

Eg.cost =15
price(“The cost is £ +str(cost))

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

How to make a string appear in a new line in just 1 print statement?

A

Use \n
Eg. print(“hello \nhow are you?”)

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

What does tab do?

A

Leaves a lot more space between the strings
Use \t
Eg. print(“hi \t how are you?)

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

How to make 2 strings that are not on the same line appear in 1 line?

A

By using end = “ “
Eg. print(“this should all print”, end = “ “)
print(“on the same line”)

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

How to make multiple strings that are not on the same line appear in 1 line?

A

U can either close all the separate strings and add a ,
Eg. print(“how will this”,
“statement”,
be printed?”)

Or u can add \ without closing the quotes
Eg. print(“This is a long line of text \
split in the middle of the quote”)

22
Q

What does triple quotes do?

A

Retains the formatting of the string
Eg.print(‘’’
John Smith
23 Elm Street
Andover
‘’’)
This makes sure it is printed out exactly like it looks in the top (in between the quote marks obviously)

23
Q

How do u write an input statement?

A

By using input(“ if there’s something u wanna say to the person typing in the input:”)
Eg. name= input(“Please enter your name:”)
print(“My name is”, name)

Or u can print the thing u wanna say first and then say variable=input()
Eg. print(“Please enter your name:”)
name = input()

24
Q

Can u use + to separate variables or numbers to strings when writing an input statement?

A

Yes (remember to turn numbers into strings tho)
Eg.name= input(“Please enter your name:”)
print(“My name is”, name)
number= input (name, + “,” + “please enter your phone number:”)

25
How to turn numbers into strings?
Use str(…) Eg. a=5 a= str(5) Or u can just put it in “ “
26
How to turn something into an integer? (Number)
Either …(int) Or in an input statement it’s int(input=…
27
How to add comments (write things to help understand what is going on in the code without it running)
Add a # Anything after this will be ignored Eg. # Chapter 2 question 1
28
How to make a string all uppercase?
variable.upper() Eg. Name = diya Print (Name.upper()) It would come out as DIYA
29
How to make a string all lowercase?
variable.lower() Eg. Name = DIYA Print (Name.lower()) It would come out as diya
30
How to replace a string with a different string?
variable.replace(old string, new string) Eg. favourite_food = Chapathi Print (Name.replace (Chapathi, Poori)) It would come out as Poori
31
What is the index?
The position of a character in a string It starts in 0 instead of 1
32
When changing a string to be uppercase,lowercase, replaced, found, indexed How do u change the original variable to be the new string instead of the old one?
At the end say Variable = variable. …(whatever you’re trying to do with it)
33
How to turn a number into a float? (With decimals)
Float (…)
34
What are the 2 ways to find the index of a character in a string? (So basically the first occurrence of a character)
variable.index(…) Finds the first time a character appears in a string Starts in 0 not 1! Variable.find(…) This gives a -1 if not found instead of error like the .index method
35
How to slice strings? (Make only part of the string appear) Eg: make it just say Pyt and not Python
Assign the string to something Then do (variable[first index:last index]) Basically but the index of the first character u want to appear and the last character u want to appear Eg. Name=Python Print(Name[0:2])
36
How to make an if statement (selection) ?
If … : elif… : else:
37
What are the 3 Boolean operators?
and or not
38
What’s the difference between Boolean operator and Boolean expression
Expression is true or false Operator is and,or,not
39
How to write a count controlled loop?
use for an in for the starting point of the iteration range to say how many times it needs to be repeated Eg. total= 0 for i in range (1,8): dailyTemp= float(input(“ what is the temperature today?”) total= float(total+ dailyTemp) print (total/7)
40
How to write a condition controlled loop?
Using while Eg. Password= input(“Please enter your password: “) while password != “DiyaDeepak123”: password= input(“Invalid password, please try again: “) print (“correct password”)
41
What is an iteration?
Repeated execution of a set of instructions
42
What is a count controlled loop?
For loop When u execute the loop a specific number of times
43
What is a condition controlled loop?
While loop When u execute the loop while a certain condition remains true
44
How to generate a random number?
Firstly type, import random, at the start of the program (don’t need to later) Then it’s Variable_name= random.randint(number u wanna start at, number you wanna end at) Eg. dice = random.randint(1,6) Any number between 1 to 6 including
45
How to write a for loop?
for … in range (starting number, ending number): The … means u can put anything u like so usually i or something Numbers are up to but not including the last number so 1,6 would be 1,2,3,4,5
46
What does while do?
True or false Usually used alongside comparison operators
47
Whats the difference between input and output?
Input is an input statement thats for the programmer by the user (receiving data) Output is the print statement thats for the user from the programmer (display the data)
48
How to write an array?
name_of_array = [None] * number of slots in memory name_of_array[0] = “whatever u wanna store there” Ps. Continue doing this until u do all the things u wanna store To find something do… name_of_array [y coordinate][x coordinate] Remember that arrays start at 0!
49
How to open an external file to read data from it and close it after?
f = open(“file name”) data = f.read() f.close()
50
What are all the stuff to search in à database and what do they do?
Variable = “SELECT name_of_column (technically called fields), FROM table_name, WHERE field > (the comparison operator) specifics
51
Whats a procedure?
When u perform a task in the background eg. Adding 1 to day or something
52
Whats a function?
When u perform a task that returns à result Eg. Print(variable)