python 学习笔记

来源:互联网 发布:非诚勿扰杨宇航淘宝店 编辑:程序博客网 时间:2024/06/08 02:39

python learning notes 1

1.list && tuple

list is similiar to array in c. But python will check if index is out of range. Also, you can index the last element by using index -1.
function:

  • append(). % add element at last
  • insert(). % insert element at pointed position
  • pop(). % erase element at pointed position.

tuple is similiar to const array in c. Once being confirmed, you can not modified the element in the tuple. So using tuple is much safer in coding. Thus, tuple has no function like pop,insert,append.

2.condition judge

Also very similiar to c, but in python, we use indentation in place of brace which we usually use. A condition statement in python express like this:

if a>5:
print('child')
elif a>10:
print('baby')
else:
print('adult')

Here, we use elif to emplace else if in c.

3.input()

Remember that from input, what we will get is always a str. If we want a interger, then we can use int() to tranform string to an int. But if you pass a str that can not be transformed to int, compiler will report an error.

4.loop

  • for loop
    syntax:for i in range:.
    range here is a list, or container that you want to traverse. If you want to traverse from 1 to 100, just write range(100), then compiler will automatically produce a list from 1 to 100. The for loop in python is just like range for loop in c++11, i just represent every element.
  • while loop
    just same as c, but it seems that we don’t have an do while in python, ha…

5.break && continue

just the same as c. No need to say. hhh.

6.dict && set

  • dict.
    dict is very similar to map in c. But its inner store is based on hash, while map in c is based on red and black tree. But it’s not important. What we need to know is that dict is also a key-value container: we use a key to calculate the proper position, then get the value we want in a short time(much faster than traverse the whole list). Syntax: Student={‘key1’:value1,’key2’:value2,…,’keyn’:valuen}. We use brace to represent a dict, bracket to represent list and parenthesis to represent tuple. Here is some functions we need to pay attentions to.

    • pop(‘key’). % remove the element using key.
    • get(‘key’,returnValue) % if key exists, return true, else return returnValue(default returnValue is None)
    • ‘key’ in dict % return true when key exists in dict, otherwise return false.

    Also, we can use index to get the value, e.g. dict[‘key1’]. But if key1 doesn’t exist, compiler will report an error. Hence, we can use two functions above to help us avoid this problem.
    Attention:key can not be changed, for dict use the value of key to calculate the index of this element. So, once we change the key in the dict, then, you know, big mass.

  • set.
    set doesn’t store value, same as the set in c. set never store the same key, that is to say, set will automatically remove the same value when you create a set. Here are some functions list.

    • remove(‘key’) % similiar to pop in dict.
    • add(‘key’) % add a new element.
    • & , | % set can be viewed as set in math, so they can do & and | operations.

    What’s more, when we new a set, we use a list to initialize this set.

7.Unchangable object

When we use replace() to change str ‘abc’ to ‘Abc’, what the compiler do acutally is new a str then replace this new str ‘abc’ with ‘Abc’. Still, the original str ‘abc’ is not changed.

原创粉丝点击