python学习笔记(一):字符串格式化、条件判断、循环、列表(含zip列表对比)

1.打印hello world

print("Hello world !")

结果:
hello world !

 2.定义不同类型的变量并打印出来

 
str = "hello world !" #str是一个字符串
num = 100 #num是一个整数
score =99.99 #score是一个小数

print(str)
print(num)
print(score)

结果:
hello world !
100
99.99

 

3.定义字符串的几种写法

#单引号
str1 = 'l love you !'
#双引号
str2 = "l love you !"

#三引号,字符串中有换行的时候可以用三引号
#三个单引号
str3 = '''l love you !
Do you love me?'''
#三个双引号
str4 = """l love you !
Do you love me?
Yes~ l love you too !"""

#字符串中有单引号,定义的时候用双引号
str5 = "I don't know"
#字符串中有双引号,定义的时候用单引号
str6 = 'He said, "He loves her too."'
#字符串中既有单引号又有双一号
str7 = '''He said, "He doesn't know."'''

4. 注释

# 两种注释方式:1.井号注释 2.三引号注释

#name = "Lucky little angel"

"""
name2 = 'Lucky little angel'
"""

'''
name3 = "Lucky little angel"
'''

5.字符串格式化

import datetime
name = "Lucky little angel"
age = 18
score = 99.99
print("我的名字叫%s" % name)
print("我今年%d了" % age)
print("我英语得了%.0f分" % score)
print("我的名字叫%s,我今年%s了,我英语得了%s分,今天的日期是:%s" %(name,age,score,datetime.datetime.today()))

结果:
我的名字叫Lucky little angel
我今年18了
我英语得了100分
我的名字叫Lucky little angel,我今年18了,我英语得了99.99分,今天的日期是:2019-05-24 16:07:57.770168

 6.输入、输出
python使用input函数来接收数据,python2中使用raw_input,接收的是一个字符串,输出使用print

name = input("请输入您的名字:")
print("%s,欢迎登陆!" % name)

结果:
请输入您的名字:Lucky little angel
Lucky little angel,欢迎登陆!

7.条件判断

age = 1
if age >= 18:
    print("成年人")
else:
    print("小屁孩")

结果:
小屁孩
score = int(input("请输入得分:"))
if score >= 90:
    print("优秀")
elif score >= 80 and score < 90:
    print("良好")
elif score >= 60 and score < 80:
    print("及格啦")
else:
    print("不及格")

结果:
请输入得分:100
优秀

8.while循环

count = 0
while count <= 5:

    if count == 3:
        count += 1
        continue

    print(count)
    count += 1

结果:
0
1
2
4
5

9.for循环

import random

for x in ('a','b','c','d','f'):

    y = random.randint(0,1)
    if y == 1:
        print("%s同学已缴费!(%s=已缴费)" % (x,y))
        continue
    else:
        print("%s同学请教续费啦!" % x)

结果:
a同学已缴费!(1=已缴费)
b同学请教续费啦!
c同学请教续费啦!
d同学已缴费!(1=已缴费)
f同学请教续费啦!

10.列表

#补充:列表对比
list1 = [1,2,3,4,5,6,7,10,11,9,4,5,6]
list2 = [1,2,3,7,8,9]

for l1,l2 in zip(list1,list2):#同时循环多个list,两个或两个以上(如果几个list的长度不一样长,那么以最短的那个list为准)
    if l1 != l2:
        print('两个表不一样的地方:%s 和 %s' % (l1,l2))
        
测试结果:
两个表不一样的地方:4 和 7
两个表不一样的地方:5 和 8
两个表不一样的地方:6 和 9
l = ['a','b','c','u']
print(l)

结果:
['a', 'b', 'c', 'u']
增加
l = ['a','b','c','u']
#在list后面增加一个元素
l.append("w")#在末尾增加
print(l)
#在list中第二个位置中增加一个x,也就是原来c待得位置
l.insert(2,"x")#在指定位置增加
print(l)

结果:
['a', 'b', 'c', 'u', 'w']
['a', 'b', 'x', 'c', 'u', 'w']
删除
l = ['a','b','c','u']

l.pop()#从末尾删除
print(l)

结果:
['a', 'b', 'c']

l.pop(2)#从指定位置删除
print(l)

结果:
['a', 'b', 'u']

l.remove("b")#删除指定的元素
print(l)

结果:
['a', 'c', 'u']
修改
l = ['a','b','c','u']
l[3] = "d"
print(l)

结果:
['a', 'b', 'c', 'd']
查找
l = ['a','b','c','u']
print(l.count("a"))#查找字符在list中存在数量(几个)
print(l.index('a'))#查找字符在list中下标位置

结果:
1
0
排序
l = ['w','a','b','c','u']

l.sort()#升序
print(l)

结果:
['a', 'b', 'c', 'u', 'w']#降序

l.sort(reverse=True)
print(l)

结果:
['w', 'u', 'c', 'b', 'a']

l.reverse()#把元素从后到前颠倒过来
print(l)

结果:
['u', 'c', 'b', 'a', 'w']
追加一个列表
l = ['w','a','b','c','u']
l2 = ['1','2','3']
l.extend(l2)#把列表l2中的元素追加到列表l中
print(l)

结果:
['w', 'a', 'b', 'c', 'u', '1', '2', '3']
遍历列表
l = ['w','a','b','c','u']
for i in l:#遍历列表的话直接把列表名写到这就好,每次遍历list的中的一个元素
    print(i)

结果:
w
a
b
c
u
多维数组取值
l = ['w','a','b','c','u',['aa','bb','cc']]
print(l[5][0])#获取二维数组中的aa

结果:
aa

l = ['w','a','b','c','u',['aa','bb','cc',['aaa','bbb','ccc','ddd']]]
print(l[5][3][1])#获取二维数组中的aa

结果:
bbb
posted @ 2019-05-24 14:57  幸运小天使  阅读(453)  评论(0编辑  收藏  举报