python基础内容和用法

一、print用法

print:一次性可以打印多个或者配合end使用,end默认不换行,里面可以加参数

print('ni','hao')
print('ni',end='')
print('ni',end='\n')

二、python的三种注释

**注释是为了方便你隔一段时间能想起你以前代码意思和方便其他人使用,引号注释一般用在开篇或函数注释。

print('xxx')#这是注释
#这是注释
"""
这是注释
"""
'''
这是注释
'''

如果#注释用在开头第一行如,则是为了方便调用系统或解释器

# coding:utf-8

三、input用法

input:接受数据,返回string类型

# coding=utf-8
name=input("请输入你的名字:")
print(name)
#返回结果
请输入你的名字:ni
ni

四、pip使用

pip:是python包的管理器,pip search报错解决方法

常用:pip install xxx模块  安装模块
pip search xxx模块  查找模块

例子:直接在dos命令使用
在这里插入图片描述

五、python变量名

总结来说就是数字、字母、下划线组成,数字不能开头,且字母区分大小写,建议用规范小写,并且不能用关键字定义变量名
常见关键字:true、false、def、elif、is、not、or、pass、raise、in、while、with、yield、import、print,请谨慎,否则程序可能出现错误或影响程序运行。

# coding:utf-8
def="ab"#这个是错误的,不能用关键字定义变量
a,b,c=1,2,3 #左右要对称
name="小明"
age=100
year_name_day='2021.9.20'
if __name__=='__main__':
   print(name,age,year_name_day)
   print(a,b,c)
# 结果
小明 100 2021.9.20
1 2 3

注:变量名要有好一些,不然其他人不容易理解,自身也容易忘记

六、python数据类型

在这里插入图片描述

七、type()

type():返回数据类型

# coding:utf-8
x=3
y=3.1415926535
print(type(x),type(y))
结果
<class 'int'> <class 'float'>

八、id()

id():查看变量的内存地址
注:如果多个变量的值相等且介于[-5,256]内,那么这些变量共用同一个值的内存空间。

例:
>>> x=-6
>>> y=-6
>>> id(x)==id(y)
False

>>> x=10
>>> y=10
>>> id(x)==id(y)
True

>>> x=257
>>> y=257
>>> id(x)==id(y)
False

九、len()

注:返回字符串长度;数字类型无法返回长度

>>> x=1
>>> print(len(x))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: object of type 'int' has no len()

>>> name="abcde"
>>> print(len(name))
5

十、in\not in

in\not in:判断数据

if  'A' in dict:
    print("A 存在")
else :
    print("A不存在")

十、max()/min()

max():返回数据中最大的成员
min():返回数据中最小的成员

>>> print(max('今天是9月1号!'))print(min('今天是9月1号!'))
!

注:中文符号>字母>数字>英文符号
中文按照拼音首字母来计算

1、+

+:用于字符串拼接

>>> x=[1,2,3]
>>> y=[4,5,6]
>>> c=x+y或直接print(x+Y)
>>> c
[1, 2, 3, 4, 5, 6]

2、bool()

bool():用于判断真假;非0非none数字返回true,其余返回false

>>> x=bool('a' in 'abc')#用于判断真假
>>> print(x)

a=0
b=0.0
c=0.1
d='none'
print(bool(a))
print(bool(b))
print(bool(c))
print(bool(d))
False
False
True
True

3、 range()

x=range(9)
# 范围为[0,9),默认为左闭右开,默认步长为1
y=range(1,9)
#范围为[1,9),默认步长为1
z=range(1,9,2)
#范围为[1,9),默认步长为2,步长可以随便修改
print(list(x))
print(list(y))
print(list(z))

D:\python\python.exe D:/python/md5.py
[0, 1, 2, 3, 4, 5, 6, 7, 8]
[1, 2, 3, 4, 5, 6, 7, 8]
[1, 3, 5, 7]

Process finished with exit code 0

4、if…else使用

1、使用print配合if…else

如果正确就输出左边,错误就输出右边

a=int(input('请输入一个整数'))
b=int(input('请输入一个整数'))
print(str(a)+'大于'+str(b) if a>b  else str(a)+'小于'+str(b))

D:\python\python.exe D:/python/md5.py
请输入一个整数1
请输入一个整数1
1大于1

Process finished with exit code 0


2、直接使用if…else

a=int(input('请输入一个整数'))
b=int(input('请输入一个整数'))
if a>b:
    print('a大于b')
elif a<b:
    print('a小于b')
else:
    print('a等于b')
   
    D:\python\python.exe D:/python/md5.py
请输入一个整数1
请输入一个整数2
a小于b

Process finished with exit code 0

3、嵌套if…else

这里运用一个购物付款作为例子

a=str(input('是否为会员y/n'))
money=float(input('请输入你的购物金额'))
if a=='y':
    if money>=1000:
        print('实付金额为:',money*0.8)
    else:
        print('实付金额为:',money*0.9)
else:
    if money>=1000:
        print('实付金额为:',money*0.95)
    else:
        print('实付金额为:',money)

5、pass

占位符:当你还没有想好下一步代码如何写时,可以使用pass先占一个位置,这样代码不会出现报错

a=1
if a>=0:
    pass
else:
    pass

6、while

当条件满足时一直运行

x=1
while x<10:
    print(x)
    x=x+1

D:\python\python.exe D:/python/md5.py
1
2
3
4
5
6
7
8
9

Process finished with exit code 0

7、for…in…

for i in 'hello world':
    print(i)
D:\python\python.exe D:/python/md5.py
h
e
l
l
o
 
w
o
r
l
d

Process finished with exit code 0

for _ in range(5):
    print('helloworld')
#如果在循环中没有使用自定义变量,可用‘-’代替
D:\python\python.exe D:/python/md5.py
helloworld
helloworld
helloworld
helloworld
helloworld

Process finished with exit code 0

*
*

本文来自B站视频内容,本人自己整理,如有冒犯,请及时告知,建议大家可以去看一下,讲解的挺详细的。
python视频内容地址

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值