Python freecodeacmp Flashcards
(22 cards)
A variable is
a name that references or points to an object. You can declare a variable by using the assignment operator = to assign a value like this:
variable_name = value
Variables can store
values of different data types.
Strings are
sequences of characters enclosed by single or double quotes, but you cannot start a string with a single quote and end it with a double quote or vice versa
print()
built-in function to print the output of your code on the terminal
Each string character can be referenced by
a numerical index. The index count starts at zero. So the first character of a string has an index of 0.
The last character has an index of
-1
the second to last has index of
-2
You can access the number of characters in a string with
the built-in len() function.
type() is
built-in function which returns the data type of a variable.
Key aspects of variable naming in Python are:
1-Some words are reserved keywords (e.g. for, while, True). They have a special meaning in Python, so you cannot use them for variable names.
2-Variable names cannot start with a number, and they can only contain alpha-numeric characters or underscores.
3-Variable names are case sensitive, i.e. my_var is different to my_Var and MY_VAR.
4-Finally, it is a common convention to write variable names using snake_case, where each space is replaced by an underscore character and the words are written in lowercase letters.
find() function:
built-in function used to locate the character, find() returns the index of the matching character inside the string. If the character is not found, it returns -1
lower() function.
transform a string into its lowercase
for i in
how to write it
indentation
Python relies on indentation to indicate blocks of code. A colon at the end of a line is a signal that a new indented block of code will follow.
An argument is
an object or an expression passed to a function — between the parentheses — when it is called. The print function can take multiple arguments, separated by a comma.
Strings are immutable, which means
they cannot be changed once created.
You can obtain the same effect of a = a + b by using
the addition assignment operator:
a += b
Comparison operators
allow you to compare two objects based on their values. You can use a comparison operator by placing it between the objects you want to compare. They return a Boolean value — namely True or False — depending on the truthfulness of the expression.
Python has the following comparison operators:
Operator Meaning
== Equal
!= Not equal
> Greater than
< Less than
≥ Greater than or equal to
≤ Less than or equal to
a conditional if statement.
This is composed of the if keyword, a condition, and a colon :.
if <condition>:
<code></code></condition>
A conditional statement can also have an else clause. This clause can be added to the end of an if statement to execute alternative code if the condition is false:
if condition:
<code>
else:
<code></code></code>
A function is
essentially a reusable block of code. You have already met some built-in functions, like print(), find() and len(). But you can also define custom functions like this:
def function_name():
<code>
A function declaration starts with the def keyword followed by the function name — a valid variable name — and a pair of parentheses. The declaration ends with a colon.</code>
To execute, a function need to be called like this:
function_name()
Now you should see the output again. Although this approach works, it doesn’t significantly enhance the code’s reusability. Repeatedly calling your function will result in the same outcome. However, functions can be declared with parameters to introduce versatility and customization:
Parameters are
variables that you can use inside your function. A function can be declared with different number of parameters. In the example above, param_1 and param_2 are parameters.
def function_name(param_1, param_2):
<code></code>
Vigenère cipher
where the offset for each letter is determined by another text, called the key.