0.安装python和pycharm
因为学习过java,所以学习python的速度会快一些,简单的代码只会敲一些,学习的视频:https://www.bilibili.com/video/BV14J411U7hj?t=309
1.hello world
print("hello world")
2.基本数据类型
price = 10 #整数
rate = 6.9 # 浮点数
name = 'Gpower' #字符串
a = b = c = 1 #python 可以同时为多个变量赋值
3.字符串
3.1 输入、字符串的拼接
name = input('what is your name? ')
print('Hi ' + name)
-----------------------------------------输出结果------------------------
what is your name? Gpwoer
Hi Gpwoer
练习 询问出生年份计算岁数
birth_year = input('Birth year: ')
age = 2020 - birth_year
print(age)
'''
报错
TypeError: unsupported operand type(s) for -: 'int' and 'str'
将str类型转换int类型
birth_year = input('Birth year: ')
age = 2020 - int(birth_year)
print(age)
同理
int()
float()
str()
补充: type():参数的类型
'''
--------------------------正确写法-------------------
birth_year = input('Birth year: ')
age = 2020 - int(birth_year)
print(age)
print(type(birth_year))
print(type(age))
-----------------------结果-------------------------
Birth year: 1999
21
<class 'str'>
<class 'int'>
3.2 关于单、双、三引号
参考:https://blog.csdn.net/woainishifu/article/details/76105667
3.3 访问字符串中的值
str = "Hello"
print(str[0])
print(str[-1])
print(str[0:3])
print(str[0:])
print(str[:3])
print(str[:])
print(str[1:-1])
'''
---------------------------输出结果------------------------------------------
H
o
Hel
Hello
Hel
Hello
ell
'''
3.4 格式化的字符
person_1 = '小红'
person_2 = '小明'
# 在字符串前面加上f,然后使用花括号动态插入值
msg = f'{person_1} 和 [{person_2}] 是好朋友'
print(msg)
-------------------------------------输出结果------------------
小红 和 [小明] 是好朋友
3.5 字符串中常见的方法
name = 'My name is Gpower'
print(name)
# len函数 判断字符串长度
print(name.title())
# title 首字母大写
print(len(name))
# uppper lower 大小写转换
print(name.upper())
print(name.lower())
#find方法对大小写敏感
print(name.find('s'))
print(name.find('S'))
print(name.find('is'))
#replace 替换且对大小写敏感
print(name.replace('Gpower','小明'))
print(name.replace('gpower','小明'))
# in 判断参数字符串是否存在原字符串 大小写敏感
print('Gpower' in name)
print('gpower' in name)
------------------------------输出结果-------------------------
My name is Gpower
My Name Is Gpower
17
MY NAME IS GPOWER
my name is gpower
9
-1
8
My name is 小明
My name is Gpower
True
False
4 整数和浮点数
4.1算术运算
print(10 + 3)
print(10 - 3)
print(10 / 3)
print(10 // 3)
print(10 % 3)
print(10 ** 3)
x = 10
x = x + 3
print(x)
# +-*/ 一样的
x += 3
print(x)
# 基本操作符优先 括号 幂 乘除 加减
y = (3 + 10) - 3**2
print(y)
---------------运行结果--------------------------
13
7
3.3333333333333335
3
1
1000
13
16
4
4.2 处理数字的函数
import math
x = 2.9
y = -1
# round 四舍五入
print(round(x))
# abs 绝对值
print(abs(-2.9))
# 还可以通过 import math 来进行向上向下取值
print(math.ceil(x))
print(math.floor(x))
----------------------------输出结果------------------------------
3
2.9
3
2
python3 math module:https://docs.python.org/3/library/math.html