DAY01 Syntax Python primer

Source: Internet
Author: User
Tags python script

One: The first line of Python code

Create the hello.py file under the/home/dev/directory, as follows:

Print ("Hello World")

 

  

Second, the Interpreter:

When python/home/dev/hello.py was executed in the previous step, it was clear where the hello.py script was executed by the Python interpreter.

If you want to execute a python script similar to executing the Shell scripting language, for example:./hello.py, then you need to synchronize the specified interpreter in the hello.py file, as follows:

#!/usr/bin/env python#-*-    utf-8    -*-print "Hello,world"

That way, you can do it directly:./hello.py can do it.

Note The problem is: when the hello.py file can not be executed, we should pay attention to execute permissions, to chmod 755 hello.py

Third, content encoding

When the Python interpreter loads the. py script file, it encodes the content. (The default ASCIII)

Ascill is a computer programming system based on the Latin alphabet, which is mainly used for displaying lines of English and other Western European languages, and he can only use 8 bits to represent one byte. A 2**8=256 character. So Ascill can only represent 256 symbols at most.

In case of Ascill not being able to represent the characters we need.

At this point Unicode (Uniform Code, universal Code, single code) is a kind of character encoding used on the computer. Unicode is created to address the limitations of traditional character encoding. However, it must be expressed in two bytes for all characters specified. This has wasted a lot of memory on Latin.

A reduced version of Unicode (condensed version) appears at this time.

utf-8: Is the compression and optimization of Unicode encoding, not using a minimum of 2 bytes. Instead, all characters and matches are categorized. The contents of the Ascill code are saved in 1 bytes, the characters in Europe are saved in 2 bytes, and the characters in East Asia are saved in 3 bytes ·

Therefore: Ascill code cannot be expressed in Chinese.

#/usr/bin/env pythonprint "Hello. World

This must be an error.

If you want to display Chinese, you have to write this:

#/usr/bin/env python#-*-coding:utf-8-*-print ("Hello, world! ");

 

Four: Notes

Single-line Comment: #

Multiline Comment: "" "" "" "

Five: Execute script incoming parameter

Python has a large number of modules, which makes the program for developing python very concise. There are three types in the class library.

>python internally supplied modules

> Industry-Open Source modules

> Programmer's Own development module

Python internally provides a SYS module, in which SYS.ARGV is used to capture the parameters passed in when executing a python script. (Sys seems to be a lot of content, I learn slowly behind it (⊙o⊙) ...)

#/usr/bin/env python#-*-coding:utf-8-*-import sysprint sys.argv

VI: PYC file.

When you execute a python code, if you import a different. py file, a. pyc file with the same name is automatically generated during execution, which is the bytecode generated by the Python interpreter after compilation.

PS: Code is compiled to generate bytecode, and bytecode can be obtained by decompile.

Seven: Variables

1. Variable declaration

#/usr/bin/env python#-*-    coding:utf-8     -*-name= "Leoday"

The appeal code declares a variable named: The value of the name variable name is: "Leoday"

The function of a variable: refers to the contents of an address saved in memory.

Rules for defining variables:

~ variable names can only be any combination of letters, numbers, or underscores.

~ The first character of a variable name cannot be a number

~ The following keywords cannot be variable names (not to be remembered)

2. Assign values to variables.

#!/usr/bin/env python #-*-    coding:utf-8    -*-name1= "Leoday" name2= "Leoday2"

  

#!/usr/bin/env python #    -*-     coding:utf-8    -*-name1= "Leoday" name2=name1

The second, name1 and name2 actually refer to the same address in memory.

Eight, input

#!/usr/bin/env python #-*-     coding:utf-8       -*-#讲用户输入的内存赋值给name变量. Name=raw_input ("Please enter user name:") #打印输入的内容print name

When entering a password, if you want to be invisible, you need to take advantage of the Getpass method in the Getpass module, namely:

#!/usr/bin/env python #-*-    coding:utf-8    -*-import getpass# assigns user-entered content to the name variable. Pwd=getpass.getpass ("Please enter password:") #打印输出的内容print pwd

Ix. Process Control and indentation

Requirement One: User Login verification:

#!/usr/bin/env python#-*-     coding:utf-8    -*-#提示输入用户名和密码 # Verify username and password # if wrong, output user name or password error # If correct, then output welcome: Xx!import Getpassname=raw_input ("Please enter user name:") pwd=getpass.getpass ("Please enter password:") if name== "Alex" and pwd== "cmd";    Print "Welcome, leoday" Else:    print "username or password Error! "

Requirement Two: Export its permissions according to user input content

#!/usr/bin/env python#-*-     coding:utf-8    -*-#根据用户输入内容打印其权限 #admin-Super admin #leoday---> General admin #leodayup-- > Business Supervisor # Other--Ordinary user name =raw_input ("Please enter username:") if name== "admin":         print "Super admin" elif name== "leoday":        print " General Administrator "elif name = =" Leodayup ":        print  " Business Administrator "else     print" Business lead "else    print" Normal user "

Getting started with day01 syntax python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.