python读书笔记(一)

一,python语法8要素

#!/usr/bin/env python3
print("hello world")

#数据类型
int("45")

str(912)

#[]使用
"Hard Times"[5]

#对象引用
x = "bule"
y = "green"
z = x

#元组使用逗号创建
tuple = ("Denmark", "Finland", "Norway","Sweden")

#in, not in 成员操作符
p = (4, "frog", 9, -33, 9, 2)
2 in p
"dog" not in p

#逻辑运算符 and or not,返回决定结果的操作数,不返回bool值
five = 5
two = 2
zero = 0
five and two
two and five
five and zero

#if语句
lines = 500
if lines < 1000:
    print("small")
elif lines < 10000:
    print("medium")
else :
    print("large")

#while语句
while True:
    item = get_next_item()
    if not item:
        break
    process_item(item)

#for in语句
countries = ["Denmark", "Finland", "Norway","Sweden"]
for countrie in countries:
    print(countrie)

#算数操作符+,-,*,/
5 + 6


#输出print
print("Type interges, each followed by Enter; or just Enter tofinish")

total = 0
count = 0

while True:
    line = input("integer:")
    if line:
        try:
            number = int (line)
        except ValueError as Error:
            print(Error)
            continue
        total += number
        count += 1
    else:
        break

if count:
    print("count = ", count,"total = ", total, "mean = ", total / count)


#函数创建及调用
def get_int(msg):
    while True:
        try:
            i = int(input(msg))
            return i
        except ValueError as err:
            print(err)

age = get_int("enter your age:")

#导入模块improt
import random
x = random.randint(1, 6)
y = random.choice(["apple", "banana", "cherry","durian"])
 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值