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.
- 【python】:python学习笔记
- Python学习笔记-Python起步
- Python学习笔记-Python基础
- Python学习笔记-Python对象
- Python基本概念--Python学习笔记
- Python学习笔记:Python函数
- python学习笔记-python安装
- Python学习笔记--Python基础
- 【Python】python基础学习笔记
- python学习笔记3
- Python学习笔记
- Boost.Python学习笔记
- python学习笔记1
- python学习笔记2
- python学习笔记(2)
- Python学习笔记 Module
- Python学习笔记.
- python学习笔记(1)
- Spring AspectJ JDBC Template
- PAT 乙级 1003
- Linux的Cgroup
- 最正确的姿势安装cudnn,网上大多数教程都太坑了
- Android高效加载大图,多图解决方案,有效避免OOM
- python 学习笔记
- linux下的虚拟内存和分页分段机制
- HDU 6061 RXD and functions(NTT+卷积)
- Android 开发之初识Kotlin
- 剑指offer:树的子结构
- 【LeetCode】004. Median of Two Sorted Arrays
- Django中ORM模型总结(二)[模型类的对应关系,模型类的属性,字段类型]
- Java-HashMap踩坑小记
- Python——切片备忘