Basics - Learn Python 3 - CodeAcademy Flashcards
Making computer print something
print(‘…’)
Variables e.g. breakfast I ate this morning
Breakfast_I_Had_This_Morning = ‘Cereal’
Print(Breakfast_I_Had_This_Morning)
THEY NEED UNDERLINES INSTEAD OF SPACES
Calculation Signs
+ - * / e.g. print(5 + 2 * 7 / 3)
Multi-Line Strings
How to make code run multiple lines without breaking
Add 3 quotation marks at start and end e.g.
‘'’Where art thou Romeo’’’
Using words as variables
Must put words in quotations
Concatenate
Adding strings together
e.g. Greeting_with_name = Greeting + ‘ ‘ + Name
Adding a comment in the code without it interfering
Place a # Before it
e.g. # This code will create a greeting
This comment will not interfere with the code
Adding to a number variable without recalculating initial variable
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
Float vs Int
Float = decimal number Int = Integer
Boolean expression
An objective statement that is either true or false e.g. monday begins with M
Relational Operators
Relational operators compare two items and return either True or False. Sometimes called Comparators
Equals: ==
Not equals: !=
If statements example
if x == y:
print(“These numbers are the same”)
Boolean Operators: Combining Boolean Expressions
and
if
not
e.g. if credits >= 120 and gpa >=3.4:
print(‘Congratulations, you can graduate!’)
Else statements
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.’)
Elif Statements
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”)
Code to generate random number
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
Concantenating Strings with Numbers/Variables
Instead of using a ‘+’ inbetween things, add a comma
e.g.
premium_ground_shipping = 125
print(‘PGS: $’,premium_ground_shipping)
Type of Error
4
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
Lists
Lists begin and end wih square brackets and have commas in between each number
e.g. heights = [61, 70, 67, 64]
Adding to lists using methods
.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
Adding lists together
Can concantenate lists using the same method as concantenating strings.
e.g. list = [‘jamie’, ‘neil’]
new_list = list + [‘duncan’, ‘rosslyn’]
Accessing list elements / drawing a value from a list
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
Selecting last element of a list
print(my_list[-1]) prints the last element of a list
-2 = second last element and so on
Removing elements from a list
my_list.remove(‘…’)
2D Lists & Selecting Elements from Them
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
Using Methods on 2D Lists .i.e. .append or .remove
put sublist you want to change after list name
e.g.
My_list = [[‘a’, 1], [‘b’, 2]]
My_list[1].remove(‘b’)
Counting number of occurrences of an element in a list
.count()
Inserting an element into a specific place in a list
.insert()
e.g. blabla.insert(2, “Steve”)
Removing an element from a specific index of a list
.pop()
Creating a sequence of integers
range()
list_of_numbers = range(10) THIS WOULD CREATE A RANGE OF 0 THROUGH 9
To print it: print(list(list_of_numbers))
Getting the length of a list
len()
e.g. my_list = [1, 2, 3, 4, 5]
print(len(my_list)) WOULD PRINT ‘5’
Sorting a list
.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