Python syntax Flashcards
Data types - in python syntax
Integer
Real
Boolean
Character
int
float
bool
str
Convention of data types into Integer Real Boolean List
Conversion is used to transform the data types of the contents of a variable using int(), str(), float(), bool() or list().
How constants conventionally named
Constants are conventionally named in all uppercase characters.
A structured data type is a sequence of items, which themselves are typed. Sequences start with an index of __.
Zero
Array vs record in python explanation + syntax
Array - A sequence of items with the same (homogeneous) data type list
Record - A sequence of items, usually of mixed (heterogenous) data types list
Arithmetic operators:
division multiplication exponentiation addition subtraction integer division modulus
/ * ** \+ - // %
Relational operators
equal to not equal to greater than greater than or equal to less than less than or equal to
== != > >= < <=
Logical/Boolean operators
- both sides of the test must be true to return true
- either side of the test must be true to return true
- inverts
and
or
not
Assignment
=
=
Selection —- all
if :
if :
else:
if :
elif :
else:
Repetition
Pre-conditioned loop. This executes while is true.
while :
Iteration -
Executes for each element of a data structure, in one dimension
for in :
Count-controlled loop. Executes a fixed number of times, based on the numbers generated by the range function
for in range (, ):
for in range (, ):
In range - what is inclusive and what is exclusive ?
for in range (, ):
Count-controlled loop. Executes a fixed number of times, based on the numbers generated by the range function
influences the numbers generated by the range function
for in range (start, stop, step):
How to define a procedure with no parameters
def ():
How to define a procedure with parameters
def (, ):
How to define a function with no parameters
def ():
return ()
How to define a function with parameters
def (, ):
return ()
Displays on the screen
print ()
Displays on the screen and returns the line typed in
input ()
Built-in subprogram:
Returns the string which matches the Unicode value of
chr ()
Displays the content of prompt to the screen and waits for the user to type in characters followed by a new line
input ()
Returns the length of the , such as a string, one-dimensional or two-dimensional data structure
len ()
Returns the integer equivalent to the Unicode string of the single character
ord ()
Prints to the display
print ()
Generates a list of numbers using , beginning with and up to, but not including, . A negative value for goes backwards
range (, , )
Rounds x to the number of n digits after the decimal (uses the 0.5 rule)
round (x, n)
List subprogram:
Adds to the end of the list
.append ()
List subprogram:
Removes the item at from list
del list[index]
List subprogram:
Inserts item just before an existing one at index
List.insert (index, item)
List subprogram:
Two methods of creating a list structure. Both are empty
aList = list ()
aList = []
String subprogram:
Returns the length of
len ()
String subprogram:
Returns the location of in the original . Returns -1, if not found
String.find (substring)
String subprogram:
Returns the location of in the original . Raises an exception if not found
String.index (substring)
String subprogram:
Returns True, if all characters are alphabetic, A–Z
.isalpha ()
String subprogram:
Returns True, if all characters are alphabetic, A–Z and digits (0–9)
.isalnum ()
String subprogram:
Returns True, if all characters are digits (0–9), exponents are digits
.isdigit ()
String subprogram:
Returns original string with all occurrences of replaced with
String.replace (s1, s2)
String subprogram:
Returns a list of all substrings in the original, using as the separator
String.split (char)
String subprogram:
Returns original string with all occurrences of char removed from the front and back
.strip (char)
String subprogram:
Returns the original string in uppercase
.upper ()
String subprogram:
Returns the original string in lowercase
.lower ()
String subprogram:
Returns True, if all characters are uppercase
string.isupper ()
String subprogram:
Returns True, if all characters are lowercase
.islower ()
String subprogram:
Formats values and puts them into the
string.format (placeholders)