Table of contents
- 1 Introduction:
- 2. Basic use of python
-
- -> 2.1 Use of comments
- -> 2.2 Variables
- -> 2.3 String related operations
- -> 2.4 console input input() method
- ->2.5 Boolean types and operations
- -> 2.6 How to use if else elif condition
- -> 2.7 random number method random.randint(a,b)
- 3. Summary case of previous grammar
This article is formally written using markdown
an editor
1 Introduction:
===> Portal: python basic environment configuration
2. Basic use of python
-> 2.1 Use of comments
—> 2.1.1 Single-line comments
Use # to indicate the space, otherwise there will be a warning
Example:
# 我是单行注释
print("helloword")
print("helloword")
—> 2.1.2 Multi-line comments
Use """ to indicate
"""
python入门01
我是多行注释
"""
-> 2.2 Variables
—> 2.2.1 Integer type/floating point type/string type
# 整数类型
666
# 浮点数字类型
13.14
# 字符串类型
"张三峰"
—> 2.2.2 Simple use of variables
Define variables and write names casually不用像java声明类型
#
money = 666
apple = 10
print("钱是多少: -> ", 666)
# 正常计算
print("剩余余额: ", money - apple, "元")
—> 2.2.3 View type and type conversion
Check the type method type (variable)
type conversion method:str(),float(),int()
# 类型转换 (执行效果跟java类似 除了语法略有不同
"""int float str 相互转换"""
num = 123
str1 = "张三"
print(type(num))
# 转换string
print(type(str(num)))
# 转换float
print(type(float(num)))
# 转换int
print(type(int(num)))
Test error
ValueError:
invalid literal for int() with base 10: 'Zhang San' `
print(type(int(str1)))
—> 2.2.4 Variable naming syntax rules
- variable name
大小写敏感
, distinguished naming - It can only start with letters, underscores, and
不能数字
at the beginning. - variable name
不能有空格
, you can use下划线
关键字,函数
can't use python- Variable name
见名知意
underscore name Chinese and English numbers underscore
# 语法规则
# 1_a= 0
a_1 = 0
_a = 0
# 变量名大小写敏感
apple = 1
Apple = 2
print(apple)
print(Apple)
# 关键字敏感
# class = 1
—>2.2.5 Mathematical (arithmetic) operators
name | symbol |
---|---|
add | + |
reduce | - |
take | * |
remove | / |
evenly divisible | // |
Take the remainder | % |
square | ** |
=> The code is as follows
print("1+1 = ", 1 + 1)
print("2-1 = ", 2 - 1)
print("3*1 = ", 3 * 1)
print("41/2 = ", 41 / 2)
print("5//2 = ", 5 // 2)
print("8%3 = ", 8 % 3)
print("8**3 = ", 8 ** 3)
# 赋值运算符 += -= *= /= %= **= //=
num = 1
num += 1
print("num += 1: ", num)
=> The result is as follows
1
2
1+1 = 2
2-1 = 1
3*1 = 3
41/2 = 20.5
5//2 = 2
8%3 = 2
8**3 = 512
-> 2.3 String related operations
—> 2.3.1 Three operations defined by string
name = '张三'
print(name)
name = "李四"
print(name)
name = """王五"""
print(name)
—> 2.3.2 String concatenation
Combination of single quotes and double quotes与java一样 转移符 "\"
nick_name = "张三'123'"
print(nick_name)
nick_name = '张三"123"'
print(nick_name)
nick_name = "张三\"123\""
print(nick_name)
ps: Special circumstances (reason for error reporting)
TypeError: can only concatenate str (not "int") to str
String concatenation (in this way only the string type can be concatenated and the digital type is not (java
different)
—> 2.3.3 String formatting
Method 1: Use %s placeholder symbol
# 字符串格式化 不同占位符 %s(字符串) %d(整数) %f(浮点数) (与java略不同)
username = "李四"
age = 18
habit = "睡觉"
money = 20.05
# print("姓名: " + username + " 年龄: " + age) # 不可以
print("姓名: " + username + " 年龄: " + str(age)) # 可以
# 使用占位符 字符串格式化
print("姓名:%s, 年龄: %s, 喜好: %s" % (username, age, habit))
print("姓名:%s, 年龄: %d, 喜好: %s" % (username, age, habit))
print("姓名:%s, 零花钱: %f, 喜好: %s" % (username, money, habit))
Method 2: Combination of quick formatting method f and {any type}
print(f"姓名:{
name}, 年龄: {
age}, 喜好: {
habit}")
# 表达式的格式化 可以不使用变量
print(f"姓名:{
name}, 年龄: {
age * 100}, 喜好类型是: {
type(habit)}")
—> 2.3.4 String formatting - digital precision control%m.nf
print("数字是112 宽度1 是 %1d" % 112)
print("数字是11.345 宽度7 小数2 是 %7.2f" % 11.345)
—>2.3.5 Floating point rounding problem
-> Lost precision problem (python writing)
Special case 11.23 (the float type is not accurate
二进制转换
, there may be四舍五入不正确
cases)
#结果是11.23 没有四舍五入
print("数字是11.235 宽度不限制 小数2 是 %.2f" % 11.235)
-> Loss of precision problem (java writing)
System.out.printf("%.2f%n", 11.235F);
-> loss of precision explained:
This principle is also mentioned in the book "Effective Java",
float
anddouble
it can only be used for scientific calculations or engineering calculations. In commercial calculations, we need to use(java)
java.math.BigDecimal
-> 2.4 console input input() method
A simplified version of Scanner similar to java
—> 2.4.1 Console input method 1:
# 控制台输入方式一:
print("姓名是什么???")
name = input()
print(f"您输入的名字是{
name}")
—> 2.4.2 Console input method two:
Note: input() collects all str type conversions, and if the conversion is incorrect, an error will be reported
#控制台输入方式二: (输入的类型就是字符串 需要什么类型就转换一下)
name = input("姓名是什么???\n")
print(f"您输入的名字是: {
name}")
print(f"您输入的类型是: {
type(name)}")
print(f"您输入的类型是: {
type(int(name))}") # 需要转换成数字就转换一下
age = input("你的年龄是: \n")
print(f"您输入的年龄是: {
age}")
print("您的名字是: %s, 您的年龄是: %s" % (name, age))
->2.5 Boolean types and operations
—> 2.5.1 Define Boolean variables
True and False, type type is bool
bool_1 = True
bool_2 = False
# bool_1的变量内容是: True,类型是<class 'bool'>
# bool_2的变量内容是: False,类型是<class 'bool'>
print(f"bool_1的变量内容是: {
bool_1},类型是{
type(bool_1)}")
print(f"bool_2的变量内容是: {
bool_2},类型是{
type(bool_2)}")
—> 2.5.2 Use of comparison operators
symbol interpretation | operator |
---|---|
equal | == |
not equal to | != |
more than the | > |
less than | < |
greater or equal to | >= |
less than or equal to | <= |
code show as below:
num1 = 1
num2 = 2
print(f"num2==num1是: {
num1 == num2}") # False
print(f"num2!=num1是: {
num1 != num2}") # True
print(f"num2>=num1是: {
num1 >= num2}") # False
print(f"num2<=num1是: {
num1 <= num2}") # True
print(f"num2>num1是: {
num1 > num2}") # False
print(f"num2<num1是: {
num1 < num2}") # True
name = "张三"
name1 = "张三"
print(f"num2==num1是: {
name == name1}") # True
-> 2.6 How to use if else elif condition
—> 2.6.1 Use of if statement
num1 = 1
if num1 > 0:
print("num1>0成立")
print("必然执行代码!")
—> 2.6.2 use if else condition
num1 = 1
if num1 > 0:
print("ok")
else:
print("error")
—> 2.6.3 if elif else conditional use
if num1 > 0:
print("big")
elif num1 == 0:
print("same")
else:
print("small")
-> 2.7 random number method random.randint(a,b)
import
Random 1-10 a = 1 b = 10 needs to be guided
# import random 导包
num = random.randint(1, 10)
print(num)
3. Summary case of previous grammar
-> 3.1 Supermarket cashier system (console version)
两种语言基础
Look at the difference between the python version and the java version
===> Portal: Introduction to python 03 Basic case python version and java version
-> 3.2 python column address
===> Portal: python language basics column
Writing is not easy. It is the first article to use markdown
writing. It is indeed much more comfortable than the rich text editor. The difficulty of getting started will be slightly higher than that of rich text, but it is also good
.