Difference between revisions of "Python"
From Centre for Bioinformatics and Computational Biology
(Replaced content with "=== Variables === === Conditionals === === Loops === === Functions === === Classes ===") |
(→Variables) |
||
Line 1: | Line 1: | ||
=== Variables === | === Variables === | ||
+ | ==== Declaration and typing ==== | ||
+ | <source lang="python"> | ||
+ | #### | ||
+ | # Boolean variables can only contain one of two values: True or False | ||
+ | # Boolean values are annotated by using the "bool" keyword | ||
+ | a: bool = True | ||
+ | #### | ||
+ | |||
+ | #### | ||
+ | # Integers are variables that contain any positive or negative whole number | ||
+ | # Integers are annotated using the "int" keyword | ||
+ | # Floats are variables that contains any positive or negative decimal | ||
+ | # Floats are annotated using the "float" keyword | ||
+ | a: int = 15 | ||
+ | b: int = -23 | ||
+ | |||
+ | c: float = 6.4 | ||
+ | d: float = -9.5 | ||
+ | #### | ||
+ | |||
+ | #### | ||
+ | # Strings are variables that contain text | ||
+ | # Strings are annotated using the "str" keyword | ||
+ | a: str = "This is a string" | ||
+ | b: str = 'Strings can be enclosed using single quotes' | ||
+ | |||
+ | # Initializing an empty string: | ||
+ | a = "" | ||
+ | #### | ||
+ | |||
+ | #### | ||
+ | # Lists are collections of other variables | ||
+ | # Lists are annotated using the "list" keyword | ||
+ | a: list = ["Some string", 15, 9.6, True] | ||
+ | |||
+ | # Initializing an empty list: | ||
+ | a = [] | ||
+ | #### | ||
+ | |||
+ | #### | ||
+ | # Sets are lists that cannot contain duplicate values | ||
+ | # They also do not keep the order of the variables | ||
+ | # Sets are a lot faster than lists when looking for specific values | ||
+ | # Sets are annotated using the "set" keyword | ||
+ | a: set = {"Some string", 15, 9.6, True} | ||
+ | |||
+ | # Initializing an empty set: | ||
+ | a = set() | ||
+ | |||
+ | #### | ||
+ | # Tuples are lists that cannot be changed after being created | ||
+ | # They are useful when returning multiple values from a function | ||
+ | # Tuples are annotated using the "tuple" keyword | ||
+ | a: tuple = ("Some string", 15, 9.6, True) | ||
+ | |||
+ | # Because tuples cannot be changed there is no point in initializing an empty one | ||
+ | #### | ||
+ | |||
+ | #### | ||
+ | # Dictionaries | ||
+ | #### | ||
+ | |||
+ | </source> | ||
+ | |||
=== Conditionals === | === Conditionals === | ||
=== Loops === | === Loops === | ||
=== Functions === | === Functions === | ||
=== Classes === | === Classes === |
Revision as of 13:43, 26 June 2019
Variables
Declaration and typing
#### # Boolean variables can only contain one of two values: True or False # Boolean values are annotated by using the "bool" keyword a: bool = True #### #### # Integers are variables that contain any positive or negative whole number # Integers are annotated using the "int" keyword # Floats are variables that contains any positive or negative decimal # Floats are annotated using the "float" keyword a: int = 15 b: int = -23 c: float = 6.4 d: float = -9.5 #### #### # Strings are variables that contain text # Strings are annotated using the "str" keyword a: str = "This is a string" b: str = 'Strings can be enclosed using single quotes' # Initializing an empty string: a = "" #### #### # Lists are collections of other variables # Lists are annotated using the "list" keyword a: list = ["Some string", 15, 9.6, True] # Initializing an empty list: a = [] #### #### # Sets are lists that cannot contain duplicate values # They also do not keep the order of the variables # Sets are a lot faster than lists when looking for specific values # Sets are annotated using the "set" keyword a: set = {"Some string", 15, 9.6, True} # Initializing an empty set: a = set() #### # Tuples are lists that cannot be changed after being created # They are useful when returning multiple values from a function # Tuples are annotated using the "tuple" keyword a: tuple = ("Some string", 15, 9.6, True) # Because tuples cannot be changed there is no point in initializing an empty one #### #### # Dictionaries ####