2. Python-based variables and data types

Well, through the last article, I believe you have finished installing python on your computer, then we are going to start learning python now. Python is really simple and efficient. Trust me, you can.
Python was actually developed by Guido von Rossum at the beginning of Christmas in 1989, probably because he did not receive a gift from Santa Claus. January 1994: Since Python 1.0 was officially released, it has reached version 3.7.x. This version is generally installed by everyone.
Ok, now open our python editor and come to the first stage of learning any language, Hello World. The code is as follows, and then click Run to get your first run result.

print("hello world")

Do you think it is super simple, then let's introduce the print method in depth. Value is what you want to output, sep is how these things are separated, end can now be simply understood as changing or not wrapping

#print()方法用于打印输出
print(value, sep=' ', end='\n')
#end默认换行,end=' '就不换行咯,

Also need to pay attention to how to output characters with double quotes embedded in them, you have to add \ to let the code know that this is what you want to output.

print('let\'s go')
print('i "love" you')
print("i \"love" you")

Well, readers of the above content can experience it in private, and output more weird things. If the output is more, it will be amazing.
After experiencing python, I feel the simplicity and charm of python, so let's learn programming from the beginning.
We knew what variables are in the math class of the third grade of elementary school. The variables in programming are similar to our mathematics. It is just a pronoun. Variables include variable names and variable values . Variable values ​​can be read and modified through variable names. For example, in python, if we make a equal to 99, we can write: a = 99.
Variable names are not random, and certain rules must be followed:
Insert picture description here
Of course, as a professional programmer, give variables (in fact, all When naming, it is also very important to know the meaning by name.
Variable values are also divided into several types, which we call data types . As shown in the figure below, generally remember the division method 2.
Insert picture description here
Here are a few examples to illustrate the type and use of variables.

"""
使用变量保存数据并进行算术运算
"""
a = 66
b = 33
print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a // b)
print(a % b)
print(a ** b)
"""
使用input()函数获取键盘输入
使用int()进行类型转换
用占位符格式化输出的字符串
"""

a = int(input('a = '))
b = int(input('b = '))
print('%d + %d = %d' % (a, b, a + b))
print('%d - %d = %d' % (a, b, a - b))
print('%d * %d = %d' % (a, b, a * b))
print('%d / %d = %f' % (a, b, a / b))
print('%d // %d = %d' % (a, b, a // b))
print('%d %% %d = %d' % (a, b, a % b))
print('%d ** %d = %d' % (a, b, a ** b))
"""
使用type()检查变量的类型

"""

a = 100
b = 12.345
c = 1 + 5j
d = 'hello, world'
e = True
print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(type(e))

The above involves the conversion between different data types:
Insert picture description here
it also involves the operation between variables, yes, it is addition and subtraction and a little more advanced. The following table lists all of them in order of priority from high to low. Operators, we will use them one after another.
Insert picture description here
The following example demonstrates the use of operators,

"""
运算符的使用

"""

a = 5
b = 10
c = 3
d = 4
e = 5
a += b
a -= c
a *= d
a /= e
print("a = ", a)

flag1 = 3 > 2
flag2 = 2 < 1
flag3 = flag1 and flag2
flag4 = flag1 or flag2
flag5 = not flag1
print("flag1 = ", flag1)
print("flag2 = ", flag2)
print("flag3 = ", flag3)
print("flag4 = ", flag4)
print("flag5 = ", flag5)
print(flag1 is True)
print(flag2 is not False)

Well, that's it for today's study. It is recommended that you practice more by yourself, and just start to get in touch with a brand new thing, even if it is simple eating, we have been learning for a long time. So have confidence, just like everyone can be a foodie, everyone can be a programmer.
There are a few exercises below to consolidate today's content. You can think about it first, try at least 15 minutes for yourself before looking at the answers.
Exercise 1: Fahrenheit to Celsius.
Hint: F = 1.8C + 32
Exercise 2: Enter the radius of the circle to calculate the circumference and area.
Hint: S=Πr²
Exercise 3: Enter the year to determine if it is a leap year.
Tip: Divisible by 400, or divisible by 4 but not divisible by 100

Answer
1.

"""
将华氏温度转换为摄氏温度

"""

f = float(input('请输入华氏温度: '))
c = (f - 32) / 1.8
print('%.1f华氏度 = %.1f摄氏度' % (f, c))
"""
输入半径计算圆的周长和面积

"""

import math

radius = float(input('请输入圆的半径: '))
perimeter = 2 * math.pi * radius
area = math.pi * radius * radius
print('周长: %.2f' % perimeter)
print('面积: %.2f' % area)
"""
输入年份 如果是闰年输出True 否则输出False

"""

year = int(input('请输入年份: '))
# 如果代码太长写成一行不便于阅读 可以使用\或()折行
is_leap = (year % 4 == 0 and year % 100 != 0 or
           year % 400 == 0)
print(is_leap)

Guess you like

Origin blog.csdn.net/qq_39748940/article/details/106472325