Sie sind auf Seite 1von 3

Python Notes

Syntax -
Set a variable using = , spam = 5, variable spam is set to 5, able to change the variable
by rewriting the code.
Boolean = A type of variable, but only True or alse.
Whitespace = indentation = ! spaces
Interpreter runs code line by line and chec"s for errors, e#ecutes the code
# = comments for humans to read$ interpreter won%t try to read as code, only wor"s for a
single line, for multiple lines, triple &uotation mar"s wor".
Editor = where we write the code
Console shows the results of the code
Print prints the code.
Math -
Addition = +
Subtraction = -
'ultiplication = (
)ivision = *
+#ponent = ((, ,((-=,.-
/Modulo = %, displays the remainder from division. 0123 = 0
or tip calculator, set variables for total,ta#,tip. Ta# and tip should be in decimal form.
'eal = meal 4 meal(ta#$ total = meal 4 meal(tip.
Strings -
trin!s can
contain letters,
numbers and
symbols, needs
to be within
&uotation
mar"s. 5ame =
6ac".
Apostrophes
confuse strings,
need a bac"slash to counter the apostrophes. )on7%t do it.
+ach character in the string is assigned a number, or inde"# The numbers start at 1.
8nde#es are written with a brac"et / 9:; after the string. +#< =yan9-; = n
>arrot = 5orwegian ?lue
len$parrot@ will give the length of the string.
5orwegian ?lue#lo%er$& will ma"e everything lowercase
5orwegian ?lue#upper$& will ma"e everything uppercase
>i=-.0!
str$pi& will turn -.0! into a string.
)ot notations, as seen in lower and upper, but not len and str, are used in codes that only
wor" with strings, where as non dot notations wor" with other data types.
Concatenation is the act of printing strings together / print Aife 4 of 4 ?rian will
print Aife of ?rian. The spaces after life and of allow the printed string to loo" li"e -
separate words. All of these elements have to be a string, use strBC@ to ma"e 'loats into
strings.
The 2 symbol may be used instead of concatenation.
5ame = 'i"e
print Dello 2s 2 Bname@ = Dello 'i"e. Ean use multiple 2s, separate Bname, :, :@
The 2s will be replaced by the variable after the 2.
Eolor = ra%(inputBave EolorC@ will promt the user with the &uestion, as a variable.
Date and Time -
dateti)e to "eep trac" of when something happened
from datetime import datetime to import the library to use
dateti)e#no%$& for current date and time
print no%#year*)onth*day to display year*month*day*hour*second.ect,
To print in mm*dd*yyyy , use string substitution.
Control Flow
Control
+lo% gives
the ability to
choose
outcomes
based on
what else is
happening in
the program.
Co)parators chec" if a value is e&ual*greater*less than another value.
e&ual to ==, not e&ual to F=, less than G, less than or e&ual to G=, greater than H, greater
than or e&ual to H=
Boolean ,peratos compare statements and result in boolean values, - operators e#ist<
and, which chec"s if both are True
or, which chec"s if atleast 0 is True
not, which gives the opposite of the statement.
There is an order of operations for booleans, not /H and /H or is last
Anything in parentheses is evaluated as its own unit
I' / conditional statement
Else - if the if statement is false, the else statement will run
Eli' = +lse 4 8f, or otherwise, if this is true, do this
if I H 3 , print :
elif I G 3 , print := otherwise, if this function is true print :
no in return co))and
-E . C,/,N .+0E1 I+*E/I+*E/E 0.0EMEN0
variable.isalphaB@ chec"s if only letters
# = Alpha
print #90<!; will print letters 0/!, lpha
pyg latin code
Functions

Das könnte Ihnen auch gefallen