1/42
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Python code general guidelines
Write one instruction per line
Indentation
Comment
Serve the purpose of offering explanations to the source code reader regarding the actions taking place within a specific code block where the comment is placed
When the code is executed, the interpreter disregards comments.
A single-line comment is initiated with the pound sign (#).
Indentation
It is of utmost importance as it indicates blocks of coded
Print Function
print (“Hello“, “World“)
Variables
they are defined and assigned whenever needed
The assignment is using the = sign
letter or underscore
Every variable should start by a ____________, that can be followed by any series of letters, digits, or underscores.
reserved words
You cannot use _______ as variable names / identifiers.
- ex. if=40
True
Variables in python are case-sensitive to letter case. 'myVariable' and 'myvariable' are seen as different variables.
int
integer numbers
the syntax /
•the result is the quotient which is a float
the syntax //
•the result is the quotient which is an integer
Float
floating-point numbers (decimal)
3.0
What does float(3) returns ?
Error
What does int("3.2") return ?
-2
What does int(-2.7) return ?
String
By default, in Python, the input function reads the user's input as a ______.
name=input("What is your name?")
output is saved in a variable, function name, one argument
int
We can convert the string output of the input function into an integer using the built-in ______ function
float
We can convert the string output of the input function into a float using the built-in ______ function
False
In Python, the code inside an f-string is not executed before it's printed.
Variable type - String
a list of characters, where each character is a letter, a digit or other symbols
String length
The number of characters in a string
greeting = HELLO
greeting[-1]
O
H
greeting = HELLO
greeting[0]
greeting[0:5]
greeting = "HELLO WORLD"
displays: 'HELLO'
displays: 'HELLO W'
greeting = "HELLO WORLD"
greeting[:7]
.upper()
converts a string to uppercase
.lower()
converts a string to lowercase
.capitalize()
converts the first letter of the string to uppercase
Decision in Python – Simple if
Statementif (condition):
instruction1_inside_if
instruction2_inside_if
instruction_outside_if
Colon (:
) → required at the end of the condition
No then
keyword → it is implicit
Indentation → determines what belongs inside the if
block
Inside the if → instructions run only if the condition is true
Outside the if → runs regardless of condition outcome
Brackets around condition: optional if simple, good practice if complex
By indentation
How does Python know which instructions belong inside the if
block?
Only if the condition is true
When are the instructions inside the if
block executed?
No, else
has no condition.
If-Else Statement
Is there a condition after else
in Python?
The if
block executes, then the instruction outside the if
. The else
block is skipped.
If-Else Statement
What happens if the condition is true?
The if
block is skipped, the else
block executes, then the instruction outside the if
.
If-Else Statement
What happens if the condition is false?
When condition1
is false and condition2
is true.
if-elif-else statement
When is the elif
block executed?
When all previous conditions are false.
if-elif-else statement
When is the else
block executed?
Exactly one block.
if-elif-else statement
In an if-elif-else
chain, how many blocks are executed?
Boolean operators
helps you make decisions with True and False. 'and' needs both to be True, 'or' needs at least one, and 'not' flips them around.
and
Operator
Meaning: Both conditions must be True → result True
False if either condition is False
Example:
x = 5
y = 10
if x > 0 and y > 5:
print("Both conditions are True")
else:
print("At least one condition is False")
and
→ all must be True
or
Operator
Meaning: At least one condition must be True → result True
False only if both conditions are False
Example:
x = 5
y = 2
if x > 0 or y > 5:
print("At least one condition is True")
else:
print("Both conditions are False")
or
→ at least one True
len(variable)
to get the length of the characters
{ }
curly brackets
If you want to add a variable in an f string, what do we put?