Basics - Learn Python 3 - CodeAcademy Flashcards

1
Q

Making computer print something

A

print(‘…’)

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

Variables e.g. breakfast I ate this morning

A

Breakfast_I_Had_This_Morning = ‘Cereal’

Print(Breakfast_I_Had_This_Morning)

THEY NEED UNDERLINES INSTEAD OF SPACES

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

Calculation Signs

A

+ - * / e.g. print(5 + 2 * 7 / 3)

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

Multi-Line Strings

How to make code run multiple lines without breaking

A

Add 3 quotation marks at start and end e.g.

‘'’Where art thou Romeo’’’

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

Using words as variables

A

Must put words in quotations

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

Concatenate

A

Adding strings together

e.g. Greeting_with_name = Greeting + ‘ ‘ + Name

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

Adding a comment in the code without it interfering

A

Place a # Before it
e.g. # This code will create a greeting

This comment will not interfere with the code

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

Adding to a number variable without recalculating initial variable

A

Add a +=
e.g. miles_i_walked_today = 10
But then I walk an additional 2 miles
Can change the variable without recalculating the initial variable like this -
miles_i_walked_today += 2
Now the value of miles_i_walked_today = 12 when I print it

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

Float vs Int

A
Float = decimal number
Int = Integer
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Boolean expression

A

An objective statement that is either true or false e.g. monday begins with M

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

Relational Operators

A

Relational operators compare two items and return either True or False. Sometimes called Comparators

Equals: ==
Not equals: !=

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

If statements example

A

if x == y:

print(“These numbers are the same”)

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

Boolean Operators: Combining Boolean Expressions

A

and
if
not

e.g. if credits >= 120 and gpa >=3.4:
print(‘Congratulations, you can graduate!’)

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

Else statements

A

Can be used to print certain commands if conditions are NOT met
e.g. if credits >= 120:
print(‘Congratulations, you can graduate!’)
else:
print (You do not have enough credits to graduate.’)

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

Elif Statements

A

Combine Else & If statements.
e.g. print(“Thank you for the donation!”)

if donation >= 1000:
print(“You’ve achieved platinum status”)
elif donation >= 500:
print(“You’ve achieved gold donor status”)
elif donation >= 100:
print(“You’ve achieved silver donor status”)
else:
print(“You’ve achieved bronze donor status”)

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

Code to generate random number

A

random_number = random.randint(1,100)

Can replace (1,100) with any numbers you want possible. e.g. (5,9) will only generate numbers 5 through 9

17
Q

Concantenating Strings with Numbers/Variables

A

Instead of using a ‘+’ inbetween things, add a comma
e.g.
premium_ground_shipping = 125
print(‘PGS: $’,premium_ground_shipping)

18
Q

Type of Error

4

A

SyntaxError: caused by not following the proper structure (syntax) of the language e.g. not putting quotation marks on a Print message

NameError: when the interpreter detects an object that is unknown/ WHEN A VARIABLE IS NOT GIVEN A VALUE.

TypeError: when an operation is applied to an object of an inappropriate type e.g. trying to add a string to an integer.

LogicError: When a programme doesn’t do what it is supposed to

19
Q

Lists

A

Lists begin and end wih square brackets and have commas in between each number
e.g. heights = [61, 70, 67, 64]

20
Q

Adding to lists using methods

A

.append(‘…’) can add an item to list.
e.g. shopping_list = [‘chocolate’, ‘sweets’]
shopping_list.append(‘ice cream’)
now if I run print(shopping_list) it will print chocolate, sweets, ice cream

21
Q

Adding lists together

A

Can concantenate lists using the same method as concantenating strings.
e.g. list = [‘jamie’, ‘neil’]
new_list = list + [‘duncan’, ‘rosslyn’]

22
Q

Accessing list elements / drawing a value from a list

A

print(name of list[3]) will print the 4th element of a list
Lists in python are ZERO-INDEXED ,’, the first element = 0
e.g. my_list = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
print(my_list[3]) will print: d

23
Q

Selecting last element of a list

A

print(my_list[-1]) prints the last element of a list

-2 = second last element and so on

24
Q

Removing elements from a list

A

my_list.remove(‘…’)

25
Q

2D Lists & Selecting Elements from Them

A

2D Lists are lists within a list e.g.
my_list = [[‘a’, 7], [‘b’, 2]]

Selecting an element from a 2D list is simple:
Jamies_number = my_list[0][1]
print(Jamies_number) = 7

26
Q

Using Methods on 2D Lists .i.e. .append or .remove

A

put sublist you want to change after list name
e.g.
My_list = [[‘a’, 1], [‘b’, 2]]
My_list[1].remove(‘b’)

27
Q

Counting number of occurrences of an element in a list

A

.count()

28
Q

Inserting an element into a specific place in a list

A

.insert()

e.g. blabla.insert(2, “Steve”)

29
Q

Removing an element from a specific index of a list

A

.pop()

30
Q

Creating a sequence of integers

A

range()
list_of_numbers = range(10) THIS WOULD CREATE A RANGE OF 0 THROUGH 9

To print it: print(list(list_of_numbers))

31
Q

Getting the length of a list

A

len()
e.g. my_list = [1, 2, 3, 4, 5]
print(len(my_list)) WOULD PRINT ‘5’

32
Q

Sorting a list

A

.sort() / sorted()

e.g. names = [bob, alex, steve, jeff]
names.sort()
print(names) would print: [alex, bob, jeff, steve]

To create a new sorted list without changing the original use SORTED
e.g. sorted(names) would create a new sorted list