Hetian lab day4 Python编程基础(上)

博客围绕Python编程基础(上)展开,包含课后题解析,如变量引用、类型及资源释放等知识;还涉及实验操作,介绍Mac系统自带Python,强调学习Python3,讲解基础语法,包括字符串、列表、元组的创建和特点。

Part 1 Python编程基础(上)课后题

在这里插入图片描述
【解析】Python的变量更像一个指针或是标签,而数值类似于对象,所以对变量的赋值类似于对对象的引用。既然是引用,主体是对象,是可以随时对其进行引用的。
变量无需事先声明,但是在引用对象前需要创建一个变量,这就好比你去山上摘桃子,总得拿个筐装桃子,创建出的这个变量就是这个筐。
变量无需指定类型,可以指定,但没必要。
Python采用del释放资源。
在这里插入图片描述
【解析】Sequence是Python的一种内置类型(built-in type),内置类型就是构建在Python Interpreter里面的类型,三种基本的Sequence Type是list(表),tuple(定值表,或翻译为元组),range(范围)。可以看作是Python Interpreter定义了这样三个class。字符串也是序列的一种。
Python序列类型包括元组、列表、字符串。
参考文档
在这里插入图片描述
【解析】我也没查到Python字符串到底是以什么结束的,但肯定不是\0。暂时记住就好了。

Part 2 Python编程基础(上)实验操作

Mac系统自带Python

我们使用命令which python3which python查看Python安装路径。
在这里插入图片描述
Python2的废弃已经进入倒计时了,到2020年,很多库都不再为Python2提供支持,所以我们学习Python2的基础语法后,主要还是学Python3吧!

基础语法

字符串string

‘str’
“str”
‘’‘str’’’
特别说明:字符串中有单引号’'则用双引号创建,有双引号则用单引号创建。

列表list=[]

列表是最常用的Python数据类型,创建一个列表中数据项不需要具有相同的类型,只要把逗号分隔的不同的数据项使用方括号括起来即可。

元组tuple=()

Python的元组与列表类似,不同之处在于元组的元素不能修改。元组使用小括号,列表使用方括号。元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可。

参考文档

1、Python 基础教程

实验操作部分代码

#coding=utf-8
print "operator"
print "-2*4+3**2=",-2*4+3**2#  #,comment; -,subtract; * multiply; +,plus; **,power; 
print "operator"
print "compare 2<4:" , 2<4#  standard operator
print "compare 6>8:" , 6>8
print "compare 6<=6:" , 6<=6
print "compare 6>=6:" , 6>=6
print "compare 6.2==6:" , 6.2==6
print "compare 6.2!=6:" , 6.2!=6
print "compare 6.2<>6:" , 6.2<>6
print
print "2<4 and 2==4" , 2<4 and 2==4#  logic operator
print "2>4 or 2<4" , 2>4 or 2<4
print "not6.2<==6" , not 6.2<=6
print "Assignment operator"
a=10
b=21
print "a=",a,"b=" , b# Assignment operator
a*=b
print "a=a*b=" , a
print "a," , type(a)# look the type of variable
print
user = raw_input('Enter login name:')# get input content
print "your login name is :",user# show input content
print "string operation"
wordS = 'word created by single quotation marks'# if '' in the string, we use "" to create string
wordD = "word created by double quotation marks"# if "" in the string, we use '' to create string
wordT = '''word created by triple quotation marks'''
print "single: ",wordS
print "double: ",wordD
print "triple: ",wordT
print "list"
list1=['Chinese' , 'Math' , 'English' , 'Chemistry',1997,2000]# there are some kinds of characters such as string, int and so on
print list1[0]# print special element
print list1[2] 
print list1# print all the list
list1[5]=2019
print list1
del list1[5]# delete special element
print list1
print "list operation"
list2=[1,2,3]
list3=[4,5,6,7]
print "list1=",list2,"list2=",list3
print "compare:",cmp(list2,list3)# if list1<list2,return -1, if list1==list2,return 0, else return 1
print "clen:",len(list2)
print "max:",max(list2)
print "min:",min(list2)
print "change string into list:",list('Hello')
print "list method"
str1='sayhello'
str2='whichis'
list4=list(str1)
list5=list(str2)
list4.append('!')#list.append(obj)add an object in the end
print "list4:",list4
print "lis4.count:",list4.count('l')#list.count(obj)count the number of a special element
print "extend in the end:",list4.extend(list5)#add another squence in the end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值