Python Intro/Syntax Flashcards
Basic Python Syntax rule:
Capitalization matters
Each statement on one line, by itself
Indentation matters!!!
Quotes around strings like “red”, no quotes around numbers
understanding errors:
Tip: read Python’s helpful error message and check for typos
SyntaxError: invalid syntax
what is print?
is a function used to display values placed inside the parentheses ( )
print(“Hi”)
print()
print(“How are you”)
“How are you” is a value that’s a string (a string of text)
what is comment?
great for annotating and adding notes to your source code.
# ##Todo: eventually I should remove the line below…
# It’s fine for now tho
“””
this is only note
“””
You can have multiple comments — as many as you want!
What is Value?
is a piece of data.
Values belong to categories called data types
Data types
String - "Hello, world!", "!" Integer - 1, 54679 Float - 3.14, -1.6 Boolean - True, False None - None
Variables
are used to store values so you can use them later
greeting = “Hello, world!”
what is =?
is a assignment operator
How to create a variable?
Valid characters are letters, numbers, and underscores
don’t start with a number
num_students
book_titles
concatenate
The act of adding one string to another with + can be referred to as string concatentation.
Strings
A string that’s an entire sentence, one character, symbols and numbers and surrounded by quotation marks.
“Hello, world!”
“a”
“! 3&*# 29”
You can use single quotes (‘) or double quotes (“)
But take care not to mismatch them!
Integers
are whole numbers that can be signed (negative) or unsigned (positive)
A positive integer: 100
A negative integer: -1568
Can separate digits with underscore: 100_000
You can perform mathematical operations on integers.
Integer operator:
Add +
Subtract -
Multiply *
Divide /
integer sample:
Adding integers together
my_score = 100 + 5
print(my_score)
num_attendees = 5
total_prizes = 20
print(total_prizes / num_attendees)
Floats
AKA floating-point numbers
They’re non-whole numbers (they can have a decimal point)
- 5
- 0.583
Booleans
The Boolean data type has just two members:
True
False
sleepy = True
print(sleepy)
>True
More Booleans samples:
sleepy = False if sleepy: print("YAWN! x(") else: print("I'm awake :)")
> I’m awake :)
None
There’s only one value that’s a None-type — None
It represents the idea of nothingness, ex.: a missing value
fav_sport = None
You’ll learn more about it later — for now just know it exists.