
variables and how to define them (a bit of LEGB rule)
using def to create or define functions
print vs return
if elif else
floating-point number vs fixed-point number
A variable is created the moment you first assign a value to it.
The type will be assigned dynamically based on the assigned value.
The form must be [variable name] = [value]
If you use the same variable name twice or more, the last one's value will be printed.
For example,
a = 'apple'
print(a)
apple
a = 'apple'
a = 'air'
print(a)
air
Python scope concept is based on LEGB rules which stands for Local, Enclosing, Global and Built-in.
Global scope: The names that you define in this scope are available to all your code.
Local scope: The names that you define in this scope are only available or visible to the code within the scope.
Defining variables inside a fuction will only work inside the function.
def to create or define functionsThe function name is followed by parameter(s) in ().
The colon : signals the start of the function body, which is marked by indentation.
Inside the function body, the return statement determines the value to be returned.
After function definition is complete, calling the function with an argument returns a value.
def add(number1, number2):
print(number1 + number2)
add (1, 2)
3
def add(number1, number 2):
return(number1 + number 2)
add(1, 2)
3
print vs returnUse return when you want to send a value from one point in your code to another.
def print_two(word1, word2):
print(word1)
print(word2)
def print_and_return(word1, word2, word3):
print_two(word3, word2)
return word1
print_two(print_and_return('B', 'C', 'D'), 'A')
D
C
B
A
D -> print
C -> print
B -> return
B -> print
A -> print
if elif elseif : True or False
elif : else if
else : rest
Using elif will make it stop running when it's true. Otherwise, the computer will run every if even if the match was found.
haven't fully understood the concept yet.