First article, Python

Source: Internet
Author: User

The following are all based on the PYTHON3 environment but (also support 2.7 only to note the syntax)

First, add the interpreter

When writing a file, you need to identify which interpreter to execute and add a Chinese interpreter.

1 #!/usr/bin/env python

2 #-*-coding:utf-8-*-

Second, the note

# When line comments """ This is a multi-line comment """ " " This is also a multi-line comment " "

Three, byte code

    • Self-brought (built-in modules)
    • Download the
    • Wrote it myself.

Write a file m.py

#/usr/bin/env python

#-*-coding:utf-8-*-

Print ("Hello word")

In writing a file hello.py

#/usr/bin/env python

#-*-coding:utf-8-*-

Import m

print "Hello"

After running, a M.PYC file is generated, which is the bytecode, and the run will continue to build after the deletion (the. dll file is a C # build file)

Iv. Declaration of variables

?     = "AA" variable, which refers to the content variable required to hold an address in memory: A character letter underscore number. Only by these components. Keyword cannot be used as a variable
View Code

Five, string

name1=== 1,name1=1 Their memory is stored in the cache so ID, when they're larger than the cache, they're pointing to a different address.

Vi. Input/Output

1) receive user input to use the Raw_input ("Please enter user name") above is 2, the following is 3 of input (" Please enter the user name ")2  = raw_input ("User input user name")3) input of the built-in module (so that the input is not visible)import= Getpass.getpass ("Please enter content") Print pwd
View Code

Seven, control flow one

Name = Raw_input (" Please enter user name:")if"Alex":  # Value comparison, memory address vs.    print" Login Successful "else:     Print " Logon Failure " double equals sign: Compare the value, not the memory of the comparison
View Code

Eight, the attention point

attention point:?    Variable declaration must first be declared before it can be used?    Notice the indentation Indentationerror?    Notice the colon? ImportGetpassname= Raw_input ("Please enter user name:") PWD= Getpass.getpass ("Please enter user name")ifName = ="a"  andPWD = ="123":    Print "Login Successful"Else:    Print "Logon Failure"The above code improvementsifPWD = ="123":   ifName = ="Eric":       Print "Eric, General"   elifName = ="Tony":       Print "Super God"   elifName = ="Alex":       Print "Chaoji"Else:    Print "Password Error"
View Code

IX. Basic Initialization data type

The basic data type is divided into two parts:?    Single-valued numeric shaping long integer floating-point complex string? Collection List meta-ancestor dictionary hash table Note Point one s%: Is the string placeholder D%: Is the number placeholder note two two ways about strings1. Name = ' I am s%,age d% '% (' alex,73')2, >>> name ="I am {0},age{1}">>>new_name=name.format ("Alex", 18)PrintNew_name I am Alex age 18Note that the three strings are divided into three characters.1, single-quoted string2, double-quoted string3, a string of three quotes (which can also be a comment) Note the index of the dot four string name="Alex"Printname[0] ' a 'PrintName[0:2] ' al 'Printname[0:] ' Alex 'PrintName[-1] ' x 'PrintName[:-1' ale ' note point Five view the length of the stringPrintLen (name) Note Point Six remove string spaces1, remove the space at both ends of the string name="Alex"PrintName.strip2, remove the space on the left side of the string to keepPrintName.lstrip ()? Remove the space on the right and leave the left blankPrintName.rstrip () Note Point Six, the split of the string split () split () the content in parentheses is what divides the string into a list>>> name ="Alex,age">>> Name.split ("a")["','Lex,','GE']
View Code

First article, Python

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.