Basic learning of Python (i)

Source: Internet
Author: User

About Python

The founder of Python for Guido van Rossum (Guido van Rossum), The birth time 1989 Christmas

I. Command rules for variables

1. Variables can only be composed of uppercase and lowercase letters, numbers and underscores, and cannot start with a number

2. Variable name must not be a keyword

For example: Pass if else print import ....

Ii. python annotations

1. Single-line Comment: #

2. Multiline comment: "Content" or "content" ""

Iii. data types for Python

1.int
2.long
3.float
4. Boolean type
5. String


string-related operations:
1. Remove blank: Str.strip (' \ n ')
The default is to remove the blank, what needs to be removed, directly in the Strip (") write it

Iv. List
name_list = [' Zhangsan ', ' Lisi ', ' Tenglan ']
name_list
type (name_list)
Basic operation of the list:
1. Index name_list[2]
2. sectioning Name_list[0:2]
3. Append name_list.append (' Alex ')
name_list.extend ([' Alex ', ' Jack '])
Name_list.insert (1, ' Lilei ')
4. Delete Name_list.pop () name_list.remove (' Alex ')
5. Length len (name_list)
6. Loop for I in Name_list:
If there is more than one Alex in a list, we want to delete all:
For i in range (Name_list.count (' Alex ')):
name_list.remove (' Alex ')

7. Include
8. Number of index values: Name_list.count (' Alex ')
9. Index of a value: Name_list.index (' Jack ')
10. Sort by: Name_list.sort ()
Invert: name_list.reverse ()

Note: Range (1)
The range here is equivalent to an iterator, unlike python2.7


Five, the tuple:
1. Index
2. Slicing
3.t.count (' Lisi '): Number of occurrences of Lisi in tuples
4.t.index (' Lisi '): Lisi subscript in tuples


Six, bitwise operations
1. Logic and:&
2. Logic or: |
3. XOR: ^
4. Take counter: ~
5. Shift left:<< left to right 0
6. Move right: >> right to left 0

Seven, logical operators
1. Logic with: and (Boolean and)
2. Logical OR: or (Boolean or)
3. Logic No: Not (Boolean not)

Eight, member operators:
1.in
2.not in

Nine, the identity operator
storage unit For comparison of two objects

1.is: Determine if two identifiers are referenced from an object
2.is not: Determine if two identifiers are referenced from different objects

10, the operation of the file:
1. Open File
file_obj = file (' path ', ' mode ') (file is already unavailable in Python3)
file_obj = open (' File path ', ' mode ')

mode of open file
1.R: Open file as read-only
2.W: Open a file for writing only, overwrite if the file exists
3.a: Opens a file for append if the file already exists, appends at the end of the file, and creates a new if the file does not exist
4.w+: writable and readable

Four, module

1.getpass

First module: Getpass[[email protected]-R1CM-SRV Preview]#Cat test_pass.py#!/USR/BIN/EVN Python3ImportGetpassusername= Input ("Username:") passwd= Getpass.getpass ("Password:")Print(USERNAME,PASSWD) [[email protected]-R1CM-SRV Preview]#Python3 test_pass.pyUsername:li Yue Meipassword:li Yue Mei000000one thing to note: When importing a module, just write the file name, no need to add py>>>ImportTest_pass.pyusername:nihaopassword:nihao000000Traceback (most recent): File"<stdin>", Line 1,inch<module>Importerror:no module named'test_pass.py';'Test_pass'  is  notA package
View Code

2.os

 >>> import   OS  >> > Os.mkdir ( '   '   '  >>> cmd_res = Os.popen ( df-ht   "  >>> print   (cmd_res) Filesystem Type Size used Avail use % mounted on  /dev/mapper/volgroup-lv_root ext4 18G 2.2G 15G 14%/tmpfs Tmpf S 242M 0 242M 0 %/dev/shm /dev/sda1 ext4 485M 33 M 427M 8%/boot 
View Code

3.sys

>>>ImportSYS>>>sys.path["','/usr/local/python/lib/python35.zip','/usr/local/python/lib/python3.5','/usr/local/python/lib/python3.5/plat-linux','/usr/local/python/lib/python3.5/lib-dynload','/usr/local/python/lib/python3.5/site-packages']>>>sys.version'3.5.1 (default, Mar, 00:36:55) \n[gcc 4.4.7 20120313 (Red Hat 4.4.7-16)]'>>>Sys.copyright'Copyright (c) 2001-2015 Python software foundation.\nall rights reserved.\n\ncopyright (c) Beopen.com.\nall Righ TS Reserved.\n\ncopyright (c) 1995-2001 Corporation for National, Initiatives.\nall rights reserved.\n\ Ncopyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.\nall rights Reserved.'>>>sys.maxsize9223372036854775807>>>Sys.prefix'/usr/local/python'>>>Sys.platform'Linux'recommended to write your own Python module storage directory:/usr/local/python/lib/python3.5/site-packages
View Code

Iv. circulation

1.if....else

Name ='Alex'passwd='alex3714'username= Input ('Please input username:') Password= Input ('Please input password:')ifUsername = =Name:Print('The user name is entered correctly. ')    ifPassword = =passwd:Print('Password entered correctly')        Print('Congratulations, the user name and password are all entered correctly. ')    Else:        Print('Password input Error')Else:    Print('Oh, even the user name did not lose the right, you are too stupid. ')
View Code

The improved program:

Name ='Alex'passwd='alex3714'username= Input ('Please input username:') Password= Input ('Please input password:')ifUsername = = Name andPassword = =passwd:Print("Welcome Login!")Else:    Print('invaild username or password,please input again!')
View Code

2.if...elif...else

Age = 18Guess_num= Int (Input ('Please input your guess number:'))ifGuess_num = =Age :Print('Congruations.you got it.')elifGuess_num >Age :Print('Think Smaller.')Else:    Print('Think bigger.')
View Code

The results of the program execution are as follows:

guess right: C:\python35\python3.exe D:/pythons13/day1/elif program. Pyplease Input You guess number:  Congruations.you got it. Small guess: C:\python35\python3.exe D:/pythons13/day1/elif program. Pyplease Input You guess number :Think bigger. Big guess: C:\python35\python3.exe D:/pythons13/day1/elif program. Pyplease Input You guess Number:Think Smaller.
View Code

3.for Loop and break usage

Age = 18 forIinchRange (10):    ifI < 3: Guess_num= Int (Input ('Please input your guess number:'))        ifGuess_num = =Age :Print('Congruations.you got it.')             Break    #don't go back, jump out of the loop        elifGuess_num >Age :Print('Think Smaller.')        Else:            Print('Think bigger.')    Else:        Print('Too Mang time attempts!')         Break
View Code

Basic learning of Python (i)

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.