Python Learning Day2

Source: Internet
Author: User

    • Variables and constants
      There is no concept of constants in Python, and if we want to define constants, they are usually represented by uppercase variable names.

    • Import PYC Parsing
      Python is a language that is compiled and interpreted in the same way as Java
      PYc is a byte-code file that the Python interpreter can read, and the last file actually runs is PYc
      PYC will make time comparisons with the original file each time the program is imported and recompile

Initial knowledge of data types
    • String

    • Digital

    • Floating point number

    • Integer

    • Regular integers

    • string concatenation of all evils

List (element start position is from 0)
  • Modification of list elements

  • list element insertion (only one can be inserted at a time)
    name = []
    Name.inster (0, ' test ')

  • List element Append
    Name.append ("Test1")

  • List element deletion
    Name.remove ("Test")

  • Global commands in Del python (any data type can be deleted)

  • Global commands in Len Python (you can see the length of the data)

  • List element Step setting
    List[0:9:2] (list lists from 0 to 9 elements read each step in two steps)

  • list element judgment, get
    9 in List #判断9是否存在list中
    List.count (3) #判断列表中有多少个3
    List.index (9) #获取元素9在列表中的下标, this method only has the subscript of the first element
    List.extend (List2) #列表扩展, append list2 to List
    List.reverse () #列表元素反转
    List.sort () #在python3中字符串不能与数字进行排序
    List.pop () #删除指定下标的元素 default Delete last element, remove is match element content delete
    Import Copy
    List.copy () = = Copy.copy () #只copy第一层, deep data structures do not copy
    Copy.deepcopy #深层次copy will copy the data exactly one copy
    Control + right mouse button (view function source code description)


Dictionary
  • Add Dict[key] = ' def ' #当key不存时添加key: ' Def ' to Dict

  • Modified Dict[key] = ' ABCD ' #当key存在是修改字典中key的值为abcd

  • Circular Dictionary

    For key in Dict:print (Key,dict[key])
  • Dict.pop (key) with del Delete
    Use Pop to delete the key specified in the dictionary
    Use del Dict[key] to have the same effect

  • Get Dictionary key value
    Dict.get (key) # no error message gets key's value, such as no key return none
    Dict[key] #获取key的value, if no key program will error

  • Dict.update (DICT2) uses DICT2 update Dict update its value when duplicate key is present, add without duplicate key

  • Dict.items () converts dict to a list,不建议使用

  • Dict.values () gets all the value in the Dict,不建议使用

  • Dict.keys () Print all current key information

  • Dict.setdefault (Key,value) returns the current value if it exists and adds this k,v if it does not exist

  • Dict.fromkeys ([1,2,3,4], ' DDD ') assigns DDD, in turn, to the elements in the list

  • Dict.popitem () randomly deletes a key from the dictionary


String
  • Strip () Cancel blank = = Shell's awk-f

  • Name.split () #name字符串转换列表

  • "". Join (list) #列表转换字符串

  • In #字符串判断
    Slices are the same as list methods

  • Name.center (+, '-') #将内容防止在四十个-the middle

  • Name.find () #查找字符下标 to find only the first value of the

  • Name.format () #不建议使用

  • Age.isdigit () #判断输入是否是字数字

    If Age.isdigit (): Age = Int (age) Else:print ("Not a number")
  • Age.alnum () #判断输入是否有特殊字符

  • Name.endswith (' DF ') #判断name是否是df结尾

  • Name.startswith (' DF ') #判断name是否是df开头

  • Name.upper () #将name字符转换为全大写

  • Name.lower () #将name字符转换为全小写


Tuples (read-only list)
    • tuple = (1,2,3,4,5) #创建元组信息

    • Tuple.count (' 1 ') #获取元组元素的数量

    • Tuple.index () #获取元组下标


This article from "Good memory than bad writing" blog, please be sure to keep this source http://zhangshaoxiong.blog.51cto.com/4408282/1774883

Python Learning Day2

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.