**`I N D E X`**
Basic Concepts
Mathematical Operations
-
To do a calculation, you have to enter it into a print statement.
-
Rational Operators (outputs are Boolean Operations)
-
Division results in a decimal at the end of your output
print((4 + 8) / 2)
-
While putting spaces around an operation sign isn’t necessary, it does make reading code easier.
-
Can use a minus sign to make a number negative (ex: -8)
-
Arithmetic Operators
In-Place Operators
go to top
In-place operators: allows code such as ‘x=x+3’ to be written more concisely, replacing the old variable being worked on, Can be used with any numerical operators (+, -, *, /, %, **, //)
x -= 2
x /= 3
x *= 5
can also be used for string concatenation
x = 'spam'
print(x)
x += 'eggs'
print(x)
Data Types
go to top
Data Name |
Definition |
Example |
String |
text enclosed in single or double quotes |
“Hello world” or ‘Hello world’ |
Integer |
whole numbers (can be used in mathematical operations) |
42 or 12 |
Float |
A decimal number, caused by division (can be used in mathematical operations) |
42.5 or 12.0 |
- when a float is added to an integer, it always comes out a float even with no remainder (ex: 12.0 + 4 = 16.0)
- dividing with a slash always causes a float, even if dividing integers
Strings Explained
- everything in quotes (”” or ‘’) is a string, even numbers
- \ : escape character (represents things in a string such as new lines, tabs, and other useful things)
- same as newline, but less repetitive, is the three quotes
- to make a string repeat itself on the same line, program print(’string’ * integer)
- cannot do string + integer
- cannot do string * string
- cannot so string * float
- adding strings together is called concatenation
Comments & Docstrings
go to top
- Comments are annotations to code using an octothorpe ( # ) to help explain what’s going on in your code or to add notes. Any text after using # will be ignored and not applied to your code
- docstrings can be done to add multiple lines of comments explaining of a function below the functions first line, acting as documentation for other developers who use your function. Unlike comments, docstrings can be inspected at run time (or when your code has been run).
Print Command
go to top