Python first day, python first day

Source: Internet
Author: User

Python first day, python first day

In the python environment, we will use 3. for Versions later than Version x, the official saying that versions above python3.x will be the language needed in the future. The Transitional versions 2.6 and 2.7 will not be updated, that is, there will be no version above 2.8, if you want to continue using python, everyone should talk about the business and move to 3. x or above. Because of teaching, it can be mixed. (In fact, I don't like mixing. Who makes others a teacher ).

Now, let's start learning:

1. First, use python to output hello world!

Go to the python Editor

# Python

>>> Print ("hello world! ")
Hello world!

2. Notes for compiling python scripts

#! /Usr/bin/env python # the interpreter of the specified script is similar to that of the shell #! /Bin/bash only in this way can the computer know the language in which you write the script

# Indent

Because the python control statement does not have a concluding sentence similar to shell:

# Shell for statements

For n in {1 .. 5}

Do

Echo $ n

 

Done

# Python for statements

For n in {1 .. 5 }:

Echo $ n

Python controls the end of a process through strict indentation

3. Variables

It can be called repeatedly without writing a large amount of code in the script, which is convenient and efficient.

(1) Rules for variable definition:

1. Only any combination of letters, numbers, and underscores can be used.

2. Variable names cannot start with numbers

3. The following keywords cannot use variable names

And, as, assert, break, class, continue, def, del, elif, else ', 'Got t', 'exec ', 'Finally', 'for ', 'from', 'global', 'if ',
'Import', 'in', 'is ', 'lambda', 'not ', 'or', 'pass', 'print', 'raise ', 'Return ', 'try', 'wait', 'with', 'yield'

(2) variable assignment

>>> Name = "zzn" # quotation marks are required if the value is a string.
>>> Print name
Zzn
>>> Age = 21 # If the value is a number, no quotation marks are required.
>>> Print age
21

Ps: we can see the id assigned to the memory through the id (variable name ).

>>> Id (name), id (age)

(39424776,394 24776)

4. User Interaction

Ps: The Interaction command of python2.x is raw_input (), and the interaction command of python3.x is input. let's talk about x. 3. x I think there is still a little far away. Foreign companies basically have 3.x

(1) Let's write an interactive statement: What is your name?

>>> Raw_input ("what are you name :")
What are you name: zzn
'Zzzzn'

The script is as follows:

# Vim test. py #! /Usr/bin/env python name = raw_input ("what are you name:") print name # python test. py what are you name: zzn

This is a small script for user interaction.

5. conditional statements

(1) if judgment

Example:

# Vim test. py #! /Usr/bin/env python name = raw_input ("what are you name:") age = raw_input ("what are you age:") if name = "zzn ": print name, age

Else:
Print ("wo cuo le! ")

# Python test. py what are you name: zzn what are you age: 21 zzn 21

(2) for statement

Example:

#!/usr/bin/env python sum = 10 for n in range(3):     sums = int(input("please you input sum:"))     if sums > sum:         print ("you sum da")     elif sums < sum:         print ("you sum xiao")     else:         print ("bingo") Break # Jump out of the entire Loop(3) while statement #!/usr/bin/env python sum = 10 cai = 0 while cai < 3:     print ("cai",cai)     sums = int(input("please you input sum:"))     if sums > sum:         print ("you sum da")     elif sums < sum:         print ("you sum xiao")     #elif sums == 10:     else:         print ("bingo")         break     cai += 1 else:     print("To many option")

6. Data Type

(1) Number

Int (integer) if you are a 32-bit system, the int value range is-2 ** 31: 2 ** 31-1:-2147483648? 2147483647 64-bit system is also true

In theory, the value range of long (long integer) is unlimited.

Float is the decimal point.

(2) Boolean Value

True or false 1 or 0

(3) String

"Hello world"

 

I. String concatenation output

# Vim test. py

#! /Usr/bin/env python
Name = raw_input ("what are you name :")
Age = raw_input ("what are you age :")
Sex = raw_input ("what are you sex :")
Job = raw_input ("what are you job :")

Print ("ni yao cha kan:" + name + "\ nname:" + name + "\ nage:" + age + "\ nsex:" + sex + "\ njob: "+ job +" \ n ")

# Python test. py

What are you name: zzn
What are you age: 24
What are you sex: n
What are you job: IT
Ni yao cha kan: zzn
Name: zzn
Age: 24
Sex: n
Job: IT

Ii. format the output

# Vim test. py

#! /Usr/bin/env python
Name = raw_input ("what are you name :")
Age = raw_input ("what are you age :")
Sex = raw_input ("what are you sex :")
Job = raw_input ("what are you job :")

Msg = """
Ni yao cha kan: % s
Name: % s
Age: % s
Sex: % s
Job: % s
"% (Name, name, age, sex, job)
Print msg

# Python test. py

Ni yao cha kan: zzn
Name: zzn
Age: 21
Sex: n
Job: IT

 

Ps: common functions of strings:

@ Remove blank space or remove a specified string

# Vim test1.py

#! /Usr/bin/env python
Name = raw_input ("what are you name:"). strip ("z") # Remove the specified string "z"
Age = raw_input ("what are you age:"). strip ()
Print name, age

# Python test1.py

What are you name: zzn
What are you age: 21 # There is a space in front of 21, but no space in the output
N 21

7. List

Dir (list) View list available parameter help (list) view Detailed help 'append' (ADD), 'Count' (statistics), 'extend' (iterator ), 'index' (index), 'insert' (insert), 'pop' (delete), 'delete' (delete), 'reverse' (reverse ), 'sorting 'exercise: >>> list = ['zzz', 'xxx', 'ccc'] >>> list ['zzz', 'xxx ', 'ccc '] view the value through the index >>> list [1] 'xxx' Add the value to the end of the list >>> list. append ("eee") >>> list ['zzz', 'xxx', 'ccc ', 'Eee'] >>> list. append ("ccc") >>> list ['zzz', 'xxx', 'ccc ', 'Eee ', 'ccc '] view the index of a value >>> list. index ("ccc ") 2 There are several statistics on a value> list. count ("ccc") 2 insert to the specified position >>> list. insert (2, "rrr") >>> list ['zzz', 'xxx', 'rrr ', 'ccc', 'eee ', 'ccc '] Delete the last value of the list >>> list. pop () 'ccc '>>> list ['zzz', 'xxx', 'rrr', 'ccc ', 'Eee'] deletes a specified value >>> list. remove ("rrr") >>> list ['zzz', 'xxx', 'ccc ', 'Eee'] reverse all values >>> list. reverse () >>> list ['Eee ', 'ccc', 'xxx', 'zzz'] >>> list. append ("! ") >>> List. append ("@") >>> list. append ("#") >>> list. append ("$") >>> list. append (11) >>> list. append (14) >>> list. append (19) sorting priority numbers followed by common characters followed by alphabetic sorting python2.x and 3. different 3.x characters and numbers cannot be sorted together> list. sort () >>> list [11, 14, 19 ,'! ',' # ',' $ ',' @ ', 'Ccc', 'Eee ', 'xxx', 'zzz'] extended list >>> list. extend ("eee") >>> list [11, 14, 19 ,'! ',' # ',' @ ', 'Ccc', 'Eee ', 'xxx', 'zzz', 'E', 'E ', 'E'] If there are more than 5000 values, how to delete all values in the list >>> list. append ("$") >>> list. append ("$") >>> list. append ("$") >>> for n in range (list. count ('$ ')):... list. remove ("$")... >>> list [11, 14, 19 ,'! ',' # ',' @ ', 'Ccc', 'Eee ', 'xxx', 'zzz'] Slice >>>> d = range (10) >>> d [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] print the first five >>> d [0: 5] [0, 1, 2, 3, 4] Even number> d [] [0, 2, 4] Cut the last two >>> d [-1:] [49] >>> d [-2:] [48, 49] include # Check whether the value is in this list >>> 20 in dTrue 8. after the tuples are defined, the list of tuples cannot be modified to convert each other >>> a [1, 2, 3, 4, 5, 'D', 'B'] >>> type () <class 'LIST' >>>> B = (1, 2, 3, 4, 5, 6) >>> B (1, 2, 3, 4, 5, 6) >>> type (B) <class 'tuple'> tuple (a) (1, 2, 3, 4, 5, 'D', 'B') 9. you can set the color and or not non-member operator in not in >>> "zzn" in name True >>> "vvv" in nameFalse >>> "vvv" not in nameTrue identity operator is not >>> x = 1 >>> x is 1 True >>> 6 is xFalse >>> 6 is not xTrue

10. File Operations

Three file formats: file('test.txt ', 'R') r read only w write only a append w + readable write without this file read mode will report an error >>> file('test.txt', 'R ') traceback (most recent call last): File "<stdin>", line 1, in <module> IOError: [Errno 2] No such file or directory: 'test.txt '>>> Write file >>> file('test.txt', 'w') <open file 'test.txt ', mode 'W' at 0x7ff3d27e6d20 >>>> file('test.txt ', 'W '). write ('df ') defines a variable for the file >>> f = file('test.txt', 'w') >>> f. write ('the good is Very \ n') Close >>> f. close () # cat testhow are you reading files >>> f = file ('test', 'R') >>> f. read () 'How are you \ n' this is my first blog. If it is hard to write or you have any suggestions, please comment and I will correct it!

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.