笔记一:python_常识

# 中文

coding:utf-8

1.在python3以上的版本中,若两侧为整数,‘/’计算结果为小数,‘//’计算结果为整数。

a = 3
b = 2
print(a/b)
print(a//b)

 运行结果如下:

D:\Python\Python37\python.exe C:/Users/Administrator/Desktop/整理CSDN/笔记1.py
1.5
1

Process finished with exit code 0

 

pyhton3以下的版本,若两侧为整数,‘/’计算结果是整数


2.阶乘运算 a**b  :a乘了b次。

a = 2
b = 3
print(a**3)

运行结果如下:

D:\Python\Python37\python.exe C:/Users/Administrator/Desktop/整理CSDN/笔记1.py
8

Process finished with exit code 0


3. 引入随机数包:import random

randint(0, 10)  0与10都包括

import random
a = random.randint(0, 10)
print(a)

运行结果如下:

D:\Python\Python37\python.exe C:/Users/Administrator/Desktop/整理CSDN/笔记1.py
9

Process finished with exit code 0


4.查看变量的类型 :type(a)  (int为整数型,float为浮点类型,str为字符串类型)

a = 666
b = 5.21
c = 'xiaoxian'
print(type(a))
print(type(b))
print(type(c))

运行结果如下:

D:\Python\Python37\python.exe C:/Users/Administrator/Desktop/整理CSDN/笔记1.py
<class 'int'>
<class 'float'>
<class 'str'>

Process finished with exit code 0


5. 转换类型:例:字符串转换成整数型:number = int(number) (number必须是纯数字,不能有特殊符号和字母)

a = '123'
print(type(a))
a = int(a)
print(type(a))

运行结果如下:

D:\Python\Python37\python.exe C:/Users/Administrator/Desktop/整理CSDN/笔记1.py
<class 'str'>
<class 'int'>

Process finished with exit code 0


6. a == b 和 a = b 的区别:前者是a与b的值相等,后者是把b的值给了a

a = 1
b = a
print(b)
if a == b:
    print('a与b相等!')

运行结果如下:

D:\Python\Python37\python.exe C:/Users/Administrator/Desktop/整理CSDN/笔记1.py
1
a与b相等!

Process finished with exit code 0


7.占位符:%d 整数占位符,%f浮点占位符(%.2f保留两位小数,以此类推),%s通用占位符。如果字符串中有两个或者两个以上的占位符,用%(占位变量1,占位变量2.....) 

a = 1
print('小娴睡了%s个小时了' % a)
a = 1.5
print('小娴睡了%f个小时了' % a)
a = 1.55
print('小娴睡了%.2f个小时了' % a)
name = '小娴'
words = '请多指教!'
print('我叫%s,我想对你说:%s' % (name, words))

运行代码如下:

D:\Python\Python37\python.exe C:/Users/Administrator/Desktop/整理CSDN/笔记1.py
小娴睡了1个小时了
小娴睡了1.500000个小时了
小娴睡了1.55个小时了
我叫小娴,我想对你说:请多指教!

Process finished with exit code 0


8.定义变量时,名字起的要见名知意,如果变量是多个单词组成,单词小写,单词之间用_下划线连接,例如猜数字 guess_number 单词太长尽量缩写


9.in 与 not in ,判断一个字符串是否在另一个字符串里面

a = '小娴'
b = '娴'
if b in a:
    print('没错,我的名字里有娴字哦!')

运行结果如下:

D:\Python\Python37\python.exe C:/Users/Administrator/Desktop/整理CSDN/笔记1.py
没错,我的名字里有娴字哦!

Process finished with exit code 0


10.return 强制结束函数执行,return之后的代码不会再继续执行  return后面也可以跟需要返回的变量值

def xiao_xian():
    word = ('这句代码会被输出!')
    return word
    print('这句代码不会不被输出')


result = xiao_xian()
print(result)

执行结果如下:

D:\Python\Python37\python.exe C:/Users/Administrator/Desktop/python整理/整理/整理CSDN/笔记1.py
这句代码会被输出!

Process finished with exit code 0


11.pass 保证代码完整性,若函数或代码块中暂无要执行的功能代码,可以使用pass占位,保证代码不出错。

def something():
    pass


something()

结果如下:

D:\Python\Python37\python.exe C:/Users/Administrator/Desktop/python整理/整理/整理CSDN/笔记1.py

Process finished with exit code 0

如果什么都不写直接调用 就会出错:

def something():


something()

结果如下:

D:\Python\Python37\python.exe C:/Users/Administrator/Desktop/python整理/整理/整理CSDN/笔记1.py
  File "C:/Users/Administrator/Desktop/python整理/整理/整理CSDN/笔记1.py", line 4
    something()
            ^
IndentationError: expected an indented block

Process finished with exit code 1


 

以上内容若有疑问和不解,请评论区留言!感谢!


 

猜你喜欢

转载自blog.csdn.net/qq_41082423/article/details/81191442
今日推荐