Skip to content

Latest commit

 

History

History
194 lines (132 loc) · 2.54 KB

lesson-10.md

File metadata and controls

194 lines (132 loc) · 2.54 KB
theme class lineNumbers info layout image
apple-basic
text-center
true
Python Basics
intro-image

Introduction to Web Development

Python Basics - modules and class


A Quick Review

We have learned:

  • Basic data types
  • while loop and for loop
  • if statement
  • Function definition

Ex 01 Write a function that satisfy :

  • func([1, 2, 3]) = 6
  • func([10, 2000, 1, 0]) = 2011
  • func([]) = 0

Arbitrary Arguments Lists

Try this.

def concat(*args, sep='/'):
    print(args)
    return sep.join(args)

print(concat('home', 'users', 'nimo'))  
# ['home', 'users', 'nimo']
# 'home/users/nimo'

The argument args captures all params to the function call.


Ex 02 Write a function that satisfy:

  • func(1, 2, 3) = 6
  • func(10, 2000, 1, 0) = 2011
  • func() = 0

Modules

  • Assuming we have two file, other.py and main.py.
  • Put the function of Ex 01 and Ex 02 in other.py
import other

print(other.func2(1, 2, 3))  # 6

Or we can use:

from other import func1, func2

print(func1([1, 2, 3, 4]))  # 10
print(func2(1, 2, 3, 4))  # 10

If there are multiple functions in one module:

from other import *

print(func1())  # 0
print(func2())  # 0

Modules

  • Importing a module will run the corresponding file
# other.py
var_name = 'value'
print('Hello from other.py')

# main.py
import other
  • You can import variables and functions from a module
# other.py
var_name = 'value'

# main.py
from other import var_name

Modules

Each module has a name.

# other.py

# main.py
import other
print(other.__name__)

If we want some code only runs when we directly run other.py

# other.py
if __name__ == '__main__':
    print('You are runing other.py')
else:
    print('You are importing other.py as a module')

Try:

>>> # in python interpreter
>>> import other
# in shell
python other.py

Modules

Nested Module

import os.path
print(os.path.abspath('.'))

Alias Name

from other import func2 as my_sum
my_sum(1, 2, 3)  # 6

Some Insteresting Modules

Run the following code in python interpreter.

>>> import sys
>>> sys.ps1
>>> sys.ps1 = '??>>'

And Try this.

>>> import this

Thanks for listening!