Python3 programming language – P1

Tram Ho


1. Introduction

  • Overview
    • Interpreted : Python is processed by the interpreter at run time.
    • Interactive : Can interact directly with the interpreter through the command line shell
    • Object-Oriented : Support object-oriented programming
    • Beginner’s Language : easy to learn even for beginners
  • Environment setup (Ubuntu)
    • Should install python version> 3.5 (to learn the Django 3.0 framework later)
    • On Ubuntu Linux, python3 is installed with just one command line: sudo apt-get install python3.7 (python 3.6 is available on ubuntu 18.04).
    • Note when installing python3.7 on ubuntu there will be many versions of python2, python3.6, by default to use version 3.7, you need to find out sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1 , sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2 , by default will automatically choose 3.7 when using the python3 command. Similarly if you want the default python command to be 3.7, you need sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.7 1
    • To be able to install more libraries for python3, need to install package manager pip3: sudo apt install -y python3-pip
    • So you can program python3, want to use any package, just install and complete the format: pip3 install package_name
  • “Hello, World” Program
    • sudo pip3.7 install virtualenv : Install this package to help create a virtual environment. From here, all newly installed packages will be placed in the new directory instead of /usr/lib/python3/package_name .
    • after creating the directory you want (the directory to code python), virtualenv -p python3.7 venv will create the venv environment directory, will not have to worry about the environment problem and just focus on the python language.
    • activate environment: source venv/bin/activate .

2. Basic Syntax

  • Easy to read syntax: print ("Hello, Python!")
  • Can interact directly with the command line or run the execute file with * .py extension, eg: python test.py
  • Python uses Indentation to define block of code, all statements in the same block must be indented by the same amount.
  • The subject line for compound statements, such as if, while, def, and class should be terminated with a colon
  • Semicolon ; is optional at the end of the statement. import sys; x = 'foo'; sys.stdout.write(x + 'n')
  • Comment with # sign or pair ''' '''


3. Datatypes and Objects

  • Number consists of integers, real numbers and complex numbers:
    • import math; x = 16; print(math.sqrt(x)) # => 4
    • import math; y = 4; print(math.pow(y, 2)) # => 16
    • print(complex(2,3)) # => (2 + 3j)
  • String:

Access

Update: the string is immutable, so it cannot be changed, surrounded by single quotes ' , double quotes " or quotes 3 '''

  • List: Similar to array data types in other languages

  • Tuple: Similar to the structure of the list, just different declared in () and immutable (like string). (quite similar to .freeze in ruby)

  • Dictionary: Similar to associative arrays in php, java, or hash in ruby

  • I / O: input and output with python shell interface


4. Expressions and Operators

  • Basic Operators The types of operators in python3:
    • Arithmetic operators + , - , * , / , % divide into remainder, ** exponentiate, // divide round
    • Comparison operator (relation): == != , < , > , <= , >=
    • Assignment operator: += , -= , *= , /= , %= , **= , //=
    • Logical operators and , or , not
    • The bit operator.
    • Member Operator: print(1 in [1,2,3,4]) # True
    • Statement operator (pointer): is , is not

  • Regular Expressions
    • The syntax of using regular expressions in python has the form: re.match(pattern, string, flags = 0) , string is the string to be processed, pattern is the regular expression used to match.
    • Basic methods: match , search , sub and split

1. Macth: match

the result is:

2. match vs search: match and search

the result is:

3. Search and Replace: search and replace

the result is:

3. Split: Split the string

result


5. Statements and Control Structures

  • Condition – Decision Making

General syntax

For example

result:


  • Loop

Syntax With for :

With while :

For example

result:

Example 2 : with range


6. Functions

declare and use

result:


Use parameters

Example 1

result:

Example 2

the results:


Anonymous function * The lamda function has no name, it will call another function. Lambda forms can take any number of arguments, but return only one value as an expression. They cannot contain commands or multiple expressions. * An anonymous function cannot call print directly because lambda requires một biểu thức .

  • Phaps: lambda [arg1 [,arg2,.....argn]]:expression
  • For example:

the results:

Global vs. Local variables

result:


7. Modules

Simply put, a module is a file that includes Python code. A module can define functions, clas, and variables (everything in python is an object). A module can also run as a script; For example, with a file named fibo.py :

The module usage is as follows:

By checking the global __name__ variable at the end of the module, you can interact directly with the file fibo.py to execute shell scripts:

Ubuntu/environments$ python fibo.py 10 produces:

You can check the functions and variables globally or locally dir() , global() , local() : with module fibo.py as follows:

result:


Combine submodules into Packages : Suppose there is a Phone folder with the module structure as follows:

  • Phone/Pots.py :

  • Phone/Isdn.py : Similar to Pots
  • Phone/G3.py : Same Pots
  • Phone/__init__.py :

  • hello.py :

or

result: python hello.py


continue P2:

    1. Classes
    1. File Handling
    1. Exceptions Handling
    1. Thematic
Share the news now

Source : Viblo