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”)