python基础

这篇博客详细介绍了Python的基础知识,包括标识符、行和缩进、字符串、变量、常见数据类型如数字、字符串、元组和列表,以及字符串的内建函数、循环、条件判断和运算符等核心概念。
摘要由CSDN通过智能技术生成

语言:

  • 编译型语言:
    1、先编译在执行
    先编译成二进制
    例子:英文书翻译成中文书再看
    c、c++、c#

  • 解释型语言:
    1、一边执行一边编译
    例子:英文书,找一个翻译,它翻译一行你听一行
    php、js、python、java、go

一、标识符

凡是在python我们自己命名,取名字的,都是标识符,包括:项目名/包名/模块名(.py文件)/变量名/函数名/类名

1. 命名规则

(1)字母.下划线.数字组成,命名时,不能以数字开头
(2)见名知意:不同字母和数字之间用下划线隔开. 如:class_basic_01
(3)项目名/包名/模块名/变量名/函数名,都是小写字母,不同的字母之间用下划线隔开
(4)类名,首字母大写,驼峰命名. 如:StudentInfo,HttpRequest
(5)不能以关键字作为标识符. 如:int,float等

二、行和缩进

三、多行语句

四、python的引号

单引号.双引号.三引号括起来的内容都是字符串

五、注释

  1. 单行注释: 快捷键ctrl+/, # 表示单行注释
# print("hello,friends!")
  1. 多行注释: ‘’’ ‘’’ 成对的三个单引号括起来的内容是多行注释
'''
name =input("请输入你的名字:")
print("你的名字是:",name)
'''

六、转义

处理字符串里的特殊字符: r R \

a = 'python\nBest'
print(a)

输出结果:
C:\Users\m\AppData\Local\Programs\Python\Python38\python.exe D:/Python/project/geng_juans_homework/day2/变量和输入输出.py
python
Best

Process finished with exit code 0

a = 'python\\nBest'
print(a)

输出结果:
C:\Users\m\AppData\Local\Programs\Python\Python38\python.exe D:/Python/project/geng_juans_homework/day2/变量和输入输出.py
python\nBest

Process finished with exit code 0
a = r'python\nBest'
print(a)

输出结果:
C:\Users\m\AppData\Local\Programs\Python\Python38\python.exe D:/Python/project/geng_juans_homework/day2/变量和输入输出.py
python\nBest

Process finished with exit code 0
a = R'python\nBest'
print(a)
输出结果:
C:\Users\m\AppData\Local\Programs\Python\Python38\python.exe D:/Python/project/geng_juans_homework/day2/变量和输入输出.py
python\nBest

Process finished with exit code 0

七、Python文件里面的输入和输出

  1. 输入

    input接收到的全都是str类型

print("hello,friends!")

输出结果:
C:\Users\m\AppData\Local\Programs\Python\Python38\python.exe D:/Python/project/python_automatic_learning/geng_juans_homework/day2/变量和输入输出.py
hello,friends!

Process finished with exit code 0
  1. 输出
name =input("请输入你的名字:")
print("你的名字是:",name)

输出结果:
C:\Users\m\AppData\Local\Programs\Python\Python38\python.exe D:/Python/project/python_automatic_learning/geng_juans_homework/day2/变量和输入输出.py
请输入你的名字:GG
你的名字是:GG

Process finished with exit code 0

八、变量

  1. 由数字、字母、下划线组成,命名时不能以数字开头
  2. 一但你创建一个变量,然后赋值,就会存在python内存里
  3. 如果要引用一个变量,那么在引用之前,要确定这个变量是否已经被定义且赋值

九、python常见数据类型

(一)数字

  1. 整数(int)
  2. 浮点数(float)

(二)字符串

1. 成对单引号,或者双引号括起来的内容,就是字符串
a = "long"
print(a)

输出结果:
C:\Users\m\AppData\Local\Programs\Python\Python38\python.exe D:/Python/project/geng_juans_homework/day2/变量和输入输出.py
long

Process finished with exit code 0
2. 字符串取值方式:字符串名[索引值]
a = "long"
print(a[1])

输出结果:
C:\Users\m\AppData\Local\Programs\Python\Python38\python.exe D:/Python/project/geng_juans_homework/day2/变量和输入输出.py
o

Process finished with exit code 0
3. 字符串里面的元素是由一个一个字符组成,字符串都是有索引的,从0开始数(正序访问),倒序访问:从-1开始
a = "long"
print(a[-1])

输出结果:
C:\Users\m\AppData\Local\Programs\Python\Python38\python.exe D:/Python/project/geng_juans_homework/day2/变量和输入输出.py
g

Process finished with exit code 0
4.字符串运算:
(1) 拼接字符串(返回数据类型是字符串型,str型):+
name = 'GG'
level = ' is Best'
print(name+level)

输出结果:
C:\Users\m\AppData\Local\Programs\Python\Python38\python.exe D:/Python/project/geng_juans_homework/day2/变量和输入输出.py
GG is Best

Process finished with exit code 0
(2)重复输出字符串(返回数据类型是字符串型,str型: *
name = 'GG '
print(name*5)

输出结果:
C:\Users\m\AppData\Local\Programs\Python\Python38\python.exe D:/Python/project/geng_juans_homework/day2/变量和输入输出.py
GG GG GG GG GG 

Process finished with exit code 0
5.判断字符串是否存在,返回布尔值(成员运算符):in/not in
(1)True

name = 'GG '
print('G'in name)

输出结果:
C:\Users\m\AppData\Local\Programs\Python\Python38\python.exe D:/Python/project/geng_juans_homework/day2/变量和输入输出.py
True

Process finished with exit code 0
(2)False
name = 'GG '
print('G'not in name)

输出结果:
C:\Users\m\AppData\Local\Programs\Python\Python38\python.exe D:/Python/project/geng_juans_homework/day2/变量和输入输出.py
False

Process finished with exit code 0

6. 不同数据类型拼接 – 数据类型转换:str(变量名)

注意:数字可转换为字符串,字符串不可转换为数字

name = 'GG'
num = 2
print(name+str(num))

输出结果:
C:\Users\m\AppData\Local\Programs\Python\Python38\python.exe D:/Python/project/geng_juans_homework/day2/变量和输入输出.py
GG2

Process finished with exit code 0
字符串格式化输出

今天的日期是xxx,我们来公布下xxx的出勤率是xxx,她这次考了xx分

1、简单粗暴直接 +


import datetime
today = datetime.datetime.today()
name = "伊伊"
rate = 99.99
score = 99
print("今天是"+str(today)+",我们来公布下"+name+"的出勤率是"+str(rate)+"她这次考了"+str(score)+"分")

输出结果:
C:\Users\m\AppData\Local\Programs\Python\Python38\python.exe D:/Python/project/geng_juans_homework/day2/格式化输出.py
今天是2020-08-14 22:26:40.744553,我们来公布下伊伊的出勤率是99.99她这次考了99分

Process finished with exit code 0

2.占位符:%s %d %f

%d:整数
%f:浮点数
%s字符串

welcome = "今天是%s,我们来公布下%s的出勤率是%f,她这次考了%d分"%(today,name,rate,score)
print(welcome)

输出结果:
C:\Users\m\AppData\Local\Programs\Python\Python38\python.exe D:/Python/project/geng_juans_homework/day2/格式化输出.py
今天是2020-08-14 22:29:02.447679,我们来公布下伊伊的出勤率是99.990000,她这次考了99分

Process finished with exit code 0
可控制保留几位小数

如保留两位小数:%.2f

score = 97.67857
welcome = "这次考了%.2f分"%(score)
print(welcome)

输出结果:
C:\Users\m\AppData\Local\Programs\Python\Python38\python.exe D:/Python/project/geng_juans_homework/day2/格式化输出.py
这次考了97.68分

Process finished with exit code 0
%d只取整数位
age = 18.5
age_str = "今年%d岁"%(age)
print(age_str)

输出结果:
C:\Users\m\AppData\Local\Programs\Python\Python38\python.exe D:/Python/project/geng_juans_homework/day2/格式化输出.py
今年18岁

Process finished with exit code 0
3 .大括号的方式
welcome2 = 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值