Python基础 第一天

本文介绍了Python的基础知识,包括使用input()获取用户输入,变量值交换,字符串操作如单引号与双引号的使用,长字符串的表示,以及字符串与数字的混合计算。此外,还讲解了条件语句的使用,如if-else和while循环,并展示了如何利用random模块生成随机数。最后,通过一个简单的猜数字游戏展示了这些概念的实际应用。
摘要由CSDN通过智能技术生成

 input用于接收用户输入

>>> input("Please input:")
Please input:2
'2'

值互换

>>> x=3
>>> y=5
>>> x,y = y,x
>>> x
5
>>> y
3

单引号与双引号,转义符与java用法一致

>>> print("Let's go")
Let's go
>>> print('"This is test"')
"This is test"
>>> print("'This\'s test'")
'This's test'

  长字符串 """

>>> t = """
test1
test2
test3
"""
>>> print(t)

test1
test2
test3

字符串与数字计算

>>> print("This is test\n"*10)
This is test
This is test
This is test
This is test
This is test
This is test
This is test
This is test
This is test
This is test

 条件语句

if 条件:

        xxx

else:

        xxx

while 条件:

        条件true时运行 代码体

 break跳出一层循环体

count = 3
while count>0:
    temp = input("请输入数字:")
    guess = int(temp)
    if guess==8:
        print("猜对了")
        break
    else:
        if(guess>8):
            print("大了")
        else:
            print("小了")
    count = count-1
print("end")

请输入数字:9
大了
请输入数字:2
小了
请输入数字:8
猜对了
end

random

random.randint(1,10)  1到10内随机数

>>> import random
>>> random.randint(1,10)
3

random.getstate() 获取之后随机数产生时的计算机内部状态

random.setstate() 将内部状态赋值

>>> import random
>>> x = random.getstate()
>>> random.randint(1,100)
72
>>> random.randint(1,100)
72
>>> random.randint(1,100)
62
>>> random.randint(1,100)
96
>>> random.setstate(x)
>>> random.randint(1,100)
72
>>> random.randint(1,100)
72
>>> random.randint(1,100)
62
>>> random.randint(1,100)
96

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值