Python First lesson

Source: Internet
Author: User

Start learning Python and keep going!

Python is a compiled language developed in C, which is then compiled into bytecode by CPython Jython IronPython and then converted to machine code in the corresponding virtual machine.

# ! /usr/bin/env python in Linux declares the use of Python parsing

#_ *_ coding:utf-8 _*_ or coding:utf-8 files are encoded in UTF-8, if not added, will be stored in ASCII code, if there is Chinese will be garbled

Import SYS imports SYS module module into system third party and custom three, if need to call external module use import

Import Getpass imported Getpass module, which has a usage getpass.getpass () user input but not displayed on the screen, can be used to enter the password

special name1 value assigned to name2 name1 name2 The value still points to name1     -- python

Loop control

If XXX:

Xxx

elif XXX:

Xxx

elif XXX:

Xxx

Else

Xxx

Variable type

1. Single value

Number-integer Long integer floating-point complex

Boolean value True False

String

Format name = ' He is%s,now are%d '% (' Alex ', 30)

Name = ' He is {0},now is {1},{2} '

Length Len ()

Removing the space Name.strip () can also remove the last newline character Lstrip () Rstrip ().

Index [] Slice [:]

Split Name.split () by default a space-delimited build list with the Join feature enables certain character substitution functions, or removes all spaces

Contains in can be used to determine whether there is (if), or to loop (for)

2. Collection

list []

Index [] Slice [:]

Add Append Delete del (does not add [] will directly delete the variable) to change the direct = Assignment

Merge '. Join ()

Contains in

Cycle

Tuple tuples () are similar to lists, but not modifiable, others are the same as lists

Dictionary dis {} is key-value Yes, read is unordered

Person = {' name ': ' Han ',' age ': Notoginseng,' gender ': ' Male ',' job ': ' Programmer ' c5>}

Print person["name"]--han

Print Person.keys ()--Return to Key list

Print person.values ()--Return value list

Print Person.items ()--Returns a list of key-value pairs, with each element being a tuple

For k,v in Person.items ():

Print K,v

Cycle

For ele in STR or list or tuple or dict:

Xxx

Break (jump out of the loop)/continue (jump out of the loop, go on to the next time)/pass (do Nothing)

While XXX:

Xxx

File operations

Opening file_obj = file ("File path", "open Mode") R W a w+

Read

File_obj.read () reads all the contents into memory

File_obj.readline () reads all of the contents into memory, with row-delimited generation lists, followed by line breaks after each entry

For lines in File_obj: Read into memory one line at a time

Write

File_obj.write ()

File_obj.writeline ()

Close File_obj.close ()

Other

Type () is used to determine the variable types to be used with is instead of = = if Type (' Han ') is str

ID () Variable memory address

Question 1: Use in to find the same principle as using the For Loop (str,list,dict), speed comparison

Python First lesson

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.