Python第一天
要点一:ubuntu的安装
以前使用启动盘安装,涉及分盘略显费事,今天用了老师的虚拟机全家桶(ubuntu里pycharm和anaconda都安好的),很好吃,VMware是个好软件。要点二:python基础学习小木鹿
1 .创建一个属于自己的代码文件
2 . 变量与交换变量
3 . 标识符小结
4 .格式化输出"%"、“.format{}”
5 .判断语句:if else esif——用它们写一个猜拳游戏
6 .whiele循环
7 .for循环与continue以及break的功能
8 . 字符串与切片
9 .字符串常见操作函数-
创建一个属于自己的代码文件——按这样操作,在右边区域修改成自己的信息即可,再次创建文件自带个人信息
变量与交换变量
a=1
b=2
print(a,b)
a,b=b,a
print(a,b)
代码效果
1 2
2 1
- 标识符小结
1 . python、c++由字母、下划线和数字组成,且不能以数字开头.python标识符区分大小写,常用下划线命名
2 .java标识符由字母、数字、美元符号$、汉字和数字组成 - %输出、format输出
age=19
print("你的年龄是:%s"%age)
name='司马懿'
print('你的名字叫{}'.format(name))
你的年龄是:19
你的名字叫司马懿
- 猜拳游戏
import random
player=input('请输入:剪刀(0)石头(1)布(2)')
player=int(player)
#生成[0,2]的随机整数
computer=random.randint(0,2)
#获胜的情况
if ((player==0 and computer==2) or (player==1 and computer==0) or (player==2 and computer==1)):
print("获胜")
#平局的情况
elif (player==computer):
("平局,敢不敢再来一局")
#输了
else:
print("输了")
请输入:剪刀(0)石头(1)布(2)>? 1
获胜
- while循环
1 .while 条件:
2 . 满足执行
#打印乘法口诀表
i=1
while i<=9:
j=1
while j<=i:
print(j,"*",i,'=',j*i,' ',end='')
j+=1
print('\n')
i+=1
1 * 1 = 1
1 * 2 = 2 2 * 2 = 4
1 * 3 = 3 2 * 3 = 6 3 * 3 = 9
1 * 4 = 4 2 * 4 = 8 3 * 4 = 12 4 * 4 = 16
1 * 5 = 5 2 * 5 = 10 3 * 5 = 15 4 * 5 = 20 5 * 5 = 25
1 * 6 = 6 2 * 6 = 12 3 * 6 = 18 4 * 6 = 24 5 * 6 = 30 6 * 6 = 36
1 * 7 = 7 2 * 7 = 14 3 * 7 = 21 4 * 7 = 28 5 * 7 = 35 6 * 7 = 42 7 * 7 = 49
1 * 8 = 8 2 * 8 = 16 3 * 8 = 24 4 * 8 = 32 5 * 8 = 40 6 * 8 = 48 7 * 8 = 56 8 * 8 = 64
1 * 9 = 9 2 * 9 = 18 3 * 9 = 27 4 * 9 = 36 5 * 9 = 45 6 * 9 = 54 7 * 9 = 63 8 * 9 = 72 9 * 9 = 81
- for循环与continue以及break的功能
1 .for 临时变量 in 可迭代的对象:
2 .循环满足时执行
3 .continue表示结束本次循环紧接着开始下一次循环
4 .break表示立刻跳出该循环
company='neusoft'
for x in company:
print("***")
if x=='s':
break
print(x)
else:
print('for循环没有执行break,则执行本语句')
***
n
***
e
***
u
***
range函数:range(起始,终止,步长)
- 字符串与切片
#截取一部分的操作
#对象【起始:终值:步长】
name='abcdef'
print(name[2])
print(name[0:3])
print(name[2:])
print(name[3:5])
print(name[1:-1])
print(name[::2])
print(name[5:1:-2])
print(name[::-1])
c
abc
cdef
de
bcde
ace
fd
fedcba
- 字符串常见操作函数
1 .find()函数
def find(self, sub, start=None, end=None):
检查某部分str是否包含在变量mystr中,如果在,返回开始的索引值,否则返回-1
my_str='hello world neuedu and neueducpp'
index1=my_str.find('neuedu',0,18)
print(index1)
12
2 .index函数
index()跟find()一样 只不过str不在mystr中要报一个异常
3 .count()函数
返回目标字符串出现的次数
count1=my_str.count('neuedu',0,19)
print(count1)
1