Python_变量字符串与随机数

Python学习_day01

初学建议使用IDLE: Integrated Development and Learing Evrionment 快乐码字

永远的 hello world

>>>print("hello world")
hello world
>>>print('hello world')
hello world
>>>print('''hello world''')
hello world

输入 import this,一起欣赏一下这首Python之诗~

>>>import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

1.1 一个猜数字的小游戏

"""用python设计一个小游戏"""
temp = input("猜一下心里想的一个数字:")
guess = int(temp)

if guess == 0:
    print("yes, 你猜对了!")
else:
    print("no, 你猜错了,正确的数字是0哦!")
print("game over!")

在这里插入图片描述
input() 函数用于接收用户的输入及返回。

tips:
Java用户记得不要写 " ; " 哦!
正确使用缩进!
正确拼写函数!

1.2 Python内置函数

输入dir(__builtins__), 查看Python的内置函数,两个下划线!
在这里插入图片描述

1.3变量 Variable

变量不可直接数字开头!
变量可以使用中文!
变量取最新值!
若要交换两个变量的值,可以不使用临时变量(第三变量),直接 x,y = y,x 即可交换!
在这里插入图片描述

1.4 字符串 String

Single quotes ’ ’
Double quotes " "
Triple quoted 三个单引号 ‘’’ ‘’’ 或 三个双引号 “”" “”"

在这里插入图片描述

引号必须成双成对!

1.5 转义字符

符号Notes
\\反斜杠(\)
\’单引号(')
\"双引号(")
\a响铃(BEL)
\b退格符(BS)
\n换行符(LF)
\t水平制表符(TAB)
\v垂直制表符(VT)
\r回车符(CR)
\f换页符(FF)
\oooooo 为八进制数
\xhhhh 为十六进制数

在这里插入图片描述
在这里插入图片描述

1.6 原始字符串 raw Strings

要输出原始字符串,在反斜杠前再加一个反斜杠,类似此笔记:
在这里插入图片描述
若要输出的东西被误以为是转义字符,只需在输出前加 r 即可:
在这里插入图片描述

⚠️ 反斜杠不可放在字符串末尾!反斜杠放在字符串的末尾,表示还没结束!
在这里插入图片描述
使用三引号,中间可无限换行,替代使用末尾反斜杠。
在这里插入图片描述

1.7 字符串的加法和乘法 Concatenation and multiplication of Strings

与其他语言一样,字符串的加法是拼接
在这里插入图片描述
乘法:可多倍输出

print("Love u 3000 tims everyday\n" * 3000)

在这里插入图片描述

运算符:
> \ < \ == \ >= \ <= \ != \ is \ is not

1.8 循环结构

While 条件:
如果条件为真 (true) 执行这里的语句
在这里插入图片描述

1.9 改进小游戏

"""用python设计一个小游戏"""
"""用python设计一个小游戏"""

import random

counts =3
answer = random.randint(0,10)
while counts > 0:
    temp = input("猜一下心里想的一个数字:")
    guess = int(temp)

    if guess == answer:
        print("yes, 你猜对了!")
        break
    else:
        if guess < answer:
            print("猜小啦")
        else:
            print("猜大啦")
        counts = counts -1
print("game over!")

在这里插入图片描述

在这里插入图片描述
1.使用break语句组织部输入正确后的无效循环。

在这里插入图片描述

2.random函数
使用 import 导入函数
random.randint(m,n) 随机打印 m 到 n 的一个数。
在这里插入图片描述

3.random 随机数重现
使用 random.setstate() 来存储随机数,使用 random.getstate() 来获取随机数。

random.getstate() 只表示取出随机数:
在这里插入图片描述
若先将第一次读取出来的随机数使用 random.setstate()保存起来再读取,便可复现上次的随机数。
在这里插入图片描述
抽奖黑幕系统原理大概如此!

Mac快捷键
Control + P:上一行
学会使用help-- Python Docs进行查找文档

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值