Python3学习笔记1-变量,数据类型

来源:互联网 发布:零基础学算法 第三版 编辑:程序博客网 时间:2024/06/10 23:52

一开始,下载、安装、学习的都是Python2.
今天在YouTube上找到一个视频, Python Tutorial for Beginners,597分钟,天哪,从基础到进阶,好详细,用的是Python3。
刚好想了解python3,顺便复习下以前看的知识。
然后然后,就想着跟着好好学学,多记笔记,以后面试会有用的。

下午看了前一个小时的视频,讲的是变量,数据类型这些基础的东西。数据类型讲了Number,String ,Dictionary,Tuples,Lists这些。

1.Python3 的print语句和python2有些不同。

>>> print('Hello World!')Hello World!>>> 

2.2.变量
a variable is just a name.
2.1 Can’t use the key word as a variable name.

Key words:and       del       from      not       whileas        elif      global    or        withassert    else      if        pass      yieldbreak     except    import    printclass     exec      in        raisecontinue  finally   is        return def       for       lambda    try来自 <http://zetcode.com/lang/python/keywords/> 

2.2 multiple assignment

2.2.1>>> var1 = var2 = var3 = "variable">>> var1'variable'>>> var2'variable'>>> var3'variable'>>> 2.2.2>>> name,age,email = "fengWeilei","22","18790166674@163.com">>> name'fengWeilei'>>> age'22'>>> email'18790166674@163.com'>>> 

2.3 syntax

>>> var 4 = "variable"SyntaxError: invalid syntax>>> >>> and = "variable"SyntaxError: invalid syntax>>>

3 Operators

"+", "-", "*", "/", "%", "**"Comparison operators<, <=, >, >=

4 Data type
4.1 Strings

>>> name = "Feng Weilei"slice function:>>> name[0]'F'>>> name[0:4]'Feng'>>> >>> name[5:]'Weilei'>>> Start at zero.0 position to 4 position, not include 4 position.5 position to the end.

4.2 Lists

>>> hobbies = ["Reading","Running","Basketball","Sleeping","Eating"]>>> hobbies[2]'Basketball'>>> hobbies[2] = "Smiling">>> hobbies['Reading', 'Running', 'Smiling', 'Sleeping', 'Eating']>>> 4.2.1 del function>>> del hobbies[3]>>> hobbies['Reading', 'Running', 'Smiling', 'Eating']>>> 4.2.2 len function>>> len(hobbies)4>>> 4.2.3 append function>>> hobbies.append("Traveling")>>> hobbies['Reading', 'Running', 'Smiling', 'Eating', 'Traveling']>>> 

4.2.4 other 10 functions of lists except append()

11 functions of a lists

4.3 Arrays

>>> array1 = [2,3,0]>>> array2 = [0,0,2]>>> array3 = array1 + array2>>> array3[2, 3, 0, 0, 0, 2]4.3.1 max function>>> max(array3)34.3.2 min function>>> min(array3)0>>> 

4.4 Dictionaries

>>> from CH1306 import CH1306>>> CH1306[311305010614]'丰伟磊'>>> len(CH1306)30>>> >>> ch1306 = CH1306.copy()>>> ch1306.clear()>>> ch1306{}>>> ch1306.update({311305010614:"丰伟磊"})>>> ch1306{311305010614: '丰伟磊'}>>> 

dict functions,missing the values()
这里写图片描述

4.5 Tuples
Can’t be changed when created.

>>> tuple1 = (1,"hello hello","python")>>> tuple2 = (2,"flask","scrapy")>>> tuple3 = tuple1+tuple2>>> tuple3(1, 'hello hello', 'python', 2, 'flask', 'scrapy')>>> tuple3[-2]'flask'>>> tuple[-2] = Flask>>> tuple[-2] = "Flask"Traceback (most recent call last):  File "<pyshell#16>", line 1, in <module>    tuple[-2] = "Flask"TypeError: 'type' object does not support item assignment