AIFFEL Day 1

Serena Chang·2021년 12월 28일
0

AIFFEL

목록 보기
1/8
post-thumbnail

LMS & Python master class day 1

What I've learned

  1. variables and how to define them (a bit of LEGB rule)

  2. using def to create or define functions

  3. print vs return

  4. if elif else

  5. floating-point number vs fixed-point number

1. Variables and how to define them

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

LEGB Rules

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.

2. Using def to create or define functions

The 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

3. print vs return

Use 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
  1. print_and_return('B', 'C', 'D')

D -> print
C -> print
B -> return

  1. print_two('B', 'A')

B -> print
A -> print

4. if elif else

if : 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.

5. floating-point number vs fixed-point number

haven't fully understood the concept yet.

profile
new to Python and everything

0개의 댓글