1. python入门杂记

python
# py是类似脚本语言,也使用#来作为注释的标志

print('--------------i love the world-----------')
temp = input("guess my number:")   #input是内嵌输入函数,返回值默认是字符串
guess = int(temp)   #把字符串转成int
if guess == 8:      # : 循环使用  判断 == 使用:结尾
    print("you are right")  
    print("No gift~.~")
else:
    print("you are wrong")
print("Game Over");
```

BIF === Built-in functions  内置函数
`dir(__builtins__)`  这条明天是查询python的内置函数
`help(input)` help类似man page, 获取函数帮助

原始字符串
```python
path=r"C:\now"
path会自动把\转义  ====>>"C:\\now"
#但是请注意  原始字符串是不能以反斜杠结尾  会提示语法错误
>>>str = r'C:\Program Files\FishC\Good''\\'     #这样可以
```
长字符串/多行
```
longstr = """niha
asdfafds"""
#此时打印longstr,会得到两行字符串
#注意  是双引号
```
与操作符  and
```
(10 < cost) and (cost < 50) 
#  注意:   当and两边都为真时,结果与C/C++不一样  要注意一下  
        (1 and 3) ====> 3
        (3 and 1) ====> 1
        (3 and 0) ====> 0
        (0 and 3) ====> 0

```
随机数模块
```
import random
secret = random.randint(1,10)   #1~10之间的数
```
类型转换
```
int()   int类型转换  但是注意,如果是int(5.99),不会四舍五入  而是直接取5
float() 浮点型
str()   字符型
```
判断变量类型
```
type("str")  ===>    <class 'str'>
type(5.2)   ===>    <class 'float'>
type(bool) ===>    <class 'type'>

temp = input("请输入一个整数:")
# 这种想法是因为type(1)会返回<class 'int'>,如果type(temp)返回结果一致说明输入是整数。
while type(temp) != type(1):
    print("抱歉,输入不合法,", end='')
    temp = input("请输入一个整数:")

isinstance("123", str)  ====>  True
isinstance("123", int)  ====>  False

temp = input("请输入一个整数:")
# 这种想法是因为type(1)会返回<class 'int'>,如果type(temp)返回结果一致说明输入是整数。
while no isinstance(temp, int):
    print("抱歉,输入不合法,", end='')
    temp = input("请输入一个整数:")   
```
python常用的操作符
```
#算数操作符
+   -   *   /(会有小数)     //(舍弃小数)      %(取余)   **(幂运算)
+=  -=  *=  /=

#值得注意的**与单目运算符优先级问题
-3**2 ===> -9
//这里注意 优先级是-(3**2),幂运算优先级更高一点

#逻辑操作符
and     or      not
3 < 4 < 5 ===> True  ===> (3<4) adn (4<5)
幂运算优先级最高,大于正负数
```
三元操作符
```
x, y = 4, 5
small = x if x < y else y
语法是:x if 条件 else y    ===>条件为真返回x,为假返回y
```
断言
```
assert 3 > 4            #====> 当条件为假时,程序自动崩溃
```
while
```
favourite = 'FishC'
for i in favourite:
    print(i, end=' ')

member = ['111', '222', '333']
for each in member:
    print(each, len(member))

for each in member:
    print(each, len(each))

for i in range(2,9,2):  #(起始,终止,步长)
    print(i)
```
列表
```
#创建
member = ['小甲鱼', '小布丁', '黑夜', '迷途']
number = [1, 2, 3, 4, 5]
mix = [1, '小甲鱼', 3.14, [1, 2, 3]]
empty = []

#添加元素
member.append('葫芦娃')     #len(member)
member.insert(1, 'gg')      #(位置, 成员)  从0开始

#删除
member.remove('bb')
member.pop('bb')    #删除并返回最后一个
member.pop(1)   #删除指定位置元素并返回

member[1:3]     #[起始,结束] 返回一个新列表
member[1:]      member[:3]      member[:]
```
列表的操作符
```
>>> list1 = [123]
>>> list2 = [234]
>>> list1 > list2
False
>>> list1=[1, 2, 3]
>>> list2=[2, 1, 1]
>>> list1 > list2
        False
list3=[1, 2, 3]
(list1<list2) and (list1 == list3)
        True
list4 = list1 + list2
>>> list4
    [1, 2, 3, 2, 1, 1]
列表还支持* 和*=等操作符,

in 和 not in
1 in list3
True
1 not in list3
False
'a' in list3
False
```
```
list3.count(123)   查看123在list3里面的次数
list3.index(123, 3, 7)  返回在3~7中,123在第几位
list3.reverse()     返转
list3.sort()  排序,
```

列表的复制操作注意事项
```
list1=list2   
只是把起了一个别名而已,两个变量名指向同一个内存,如果list1.reverse(), 那个list2里面的内容也是反转之后的
list3=list2[:]
是重新生成一块内存来存储list2[:]的分片,内存是独立的
```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值