Python100Days study notes --- Day2 language elements

Day2

Language elements
instructions and programs
a computer hardware system typically consists of five members, comprising: a computing unit, a controller, a memory, input and output devices. Wherein the controller and operator together what we usually call a central processor, its function is to perform various calculations and control instructions and data processing computer software. We usually refer to the program is actually a collection of instructions, we will program is a series of instructions in a certain way to organize together, and then to control the computer to do what we want it to do by these instructions. Today we use the computer most of the time, although they work more and more sophisticated components, processing power and more powerful, but in essence it is still a computer "von Neumann architecture". "Von Neumann architecture" There are two key points, one point out To separate storage device and a central processing unit, the second is made to data in binary coded. Binary is a "binary every one" counting method, there is no substantive difference with "every decade a" counting method we use in humans, because humans have ten fingers so using decimal (because the count exhausted after ten fingers can only carry, of course, everything has an exception, the Maya may be due to many years of barefoot toes also count, so they use the counting method Vigesimal in this Mayan calendar on different kinds of counting under the guidance of the calendar we normally use, but according to the Mayan calendar, 2012 is on a so-called "sun Ji" last year, and 2013 is the new start "sun Ji", then this thing is wrongly informed way misrepresented as "2012 Mayan doomsday prophecy" this myth, today we can safely guess, Maya civilization have been slow to estimated and used two decimal related). For computers, binary physically device is most easily achieved (1 denotes a high voltage, low voltage represents 0), then the computer "von Neumann architecture" are used in binary. While we do not need every programmer can use the binary way of thinking to work, but to understand the relationship between binary and convert it to life in our decimal, binary, and conversion relationship with the octal and hexadecimal still necessary. If you are not familiar with this point, you can use on their own or Baidu Encyclopedia Wikipedia about science.

Tip: Recent studies on quantum computers has been pushed to the cusp, a quantum computer based on quantum mechanics calculation, to convey information using quantum teleport way. June 2018, Intel announced the development of new quantum chip and passed the test at near absolute zero environment; 2019, IBM and Google have launched their own quantum computer.

Variables and types
in programming, the carrier is a variable for storing data. The variables in the computer data actually present is a memory or a memory space for storing data, the value of the variable can be read and modified, which is the basis of all calculations and control. Data from a computer can have a variety of types, in addition to a variety of values may also process data text, graphics, audio, video, etc., the different data requires different storage types defined. Many data types in Python, but also allows us to customize the new data types (that will be mentioned later), we first introduce several common data types.

整型:Python中可以处理任意大小的整数(Python 2.x中有intlong两种类型的整数,但这种区分对Python来说意义不大,因此在Python 3.x中整数只有int这一种了),而且支持二进制(如0b100,换算成十进制是4)、八进制(如0o100,换算成十进制是64)、十进制(100)和十六进制(0x100,换算成十进制是256)的表示法。
浮点型:浮点数也就是小数,之所以称为浮点数,是因为按照科学记数法表示时,一个浮点数的小数点位置是可变的,浮点数除了数学写法(如123.456)之外还支持科学计数法(如1.23456e2)。
字符串型:字符串是以单引号或双引号括起来的任意文本,比如'hello'"hello",字符串还有原始字符串表示法、字节字符串表示法、Unicode字符串表示法,而且可以书写成多行的形式(用三个单引号或三个双引号开头,三个单引号或三个双引号结尾)。
布尔型:布尔值只有True、False两种值,要么是True,要么是False,在Python中,可以直接用True、False表示布尔值(请注意大小写),也可以通过布尔运算计算出来(例如3 < 5会产生布尔值True,而2 == 1会产生布尔值False)。
复数型:形如3+5j,跟数学上的复数表示一样,唯一不同的是虚部的i换成了j。实际上,这个类型并不能算作常用类型,大家了解下就可以了。

Variable names
for each variable we need to give it a name, just as each of us has his own famous name the same. In Python, variable naming need to follow these rules must comply with rigid and non-rigid rules is strongly recommended to follow.

Hard and fast rule:
variable name by the letter (generalized Unicode characters, not including special characters), numbers, and the underscore, numbers can not begin.
Case-sensitive (uppercase and a lowercase A are two different variables).
Do not tell keywords (words that have special meaning, will be mentioned later) and system reserved words (such as the name of the function, module, etc.) conflict.
PEP 8 Requirements:
lowercase spelling, multiple words connected with underscores.
Protected beginning single instance attribute underlined (will be mentioned later).
Private instance attribute start with two underscores (will be mentioned later).
Of course, as a professional programmer, to a variable (in fact it should be all identifiers) do see the name meaning is also very important to know when naming.

Variable is used
below to illustrate the type of variables and variables used by several examples.

"""
使用变量保存数据并进行算术运算

Version: 0.1
Author: 骆昊
"""

a = 321
b = 123
print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a // b)
print(a % b)
print(a ** b)

Type may be used in Python function type variables are checked. The concept of a mathematical function concept with programming function is the same, the mathematical function I believe we no stranger, which includes the function name, independent and dependent variables. If you temporarily do not understand this concept does not matter, we will be devoted to define and use functions in later chapters.

"""
使用type()检查变量的类型

Version: 0.1
Author: 骆昊
Date: 2018-02-27
"""

a = 100
b = 12.345
c = 1 + 5j
d = 'hello, world'
e = True
print(type(a)) # <class 'int'>
print(type(b)) # <class 'float'>
print(type(c)) # <class 'complex'>
print(type(d)) # <class 'str'>
print(type(e)) # <class 'bool'>

Variables can be used to convert the Python built-in functions.

int():将一个数值或字符串转换成整数,可以指定进制。
float():将一个字符串转换成浮点数。
str():将指定的对象转换成字符串形式,可以指定编码。
chr():将整数转换成该编码对应的字符串(一个字符)。
ord():将字符串(一个字符)转换成对应的编码(整数)。

The following code through the keyboard of the arithmetic operation is achieved two inputs two integers integers.

"""
使用input()函数获取键盘输入(字符串)
使用int()函数将输入的字符串转换成整数
使用print()函数输出带占位符的字符串

Version: 0.1
Author: 骆昊
"""

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))
说明:上面的print函数中输出的字符串使用了占位符语法,其中%d是整数的占位符,%f是小数的占位符,%%表示百分号(因为百分号代表了占位符,所以带占位符的字符串中要表示百分号必须写成%%),字符串之后的%后面跟的变量值会替换掉占位符然后输出到终端中,运行上面的程序,看看程序执行结果就明白啦。

Operators
Python supports multiple operators, substantially according to the following table in descending order of priority lists all operators, operator precedence referring to a plurality of operators occur simultaneously, and then calculating what to do first What do arithmetic. In addition Before we have used the assignment operator and arithmetic operators, we will continue to use other operators mentioned later.

运算符	描述
[] [:]	下标,切片
**	指数
~ + -	按位取反, 正负号
* / % //	乘,除,模,整除
+ -	加,减
>> <<	右移,左移
&	按位与
^ \|	按位异或,按位或
<= < > >=	小于等于,小于,大于,大于等于
== !=	等于,不等于
is is not	身份运算符
in not in	成员运算符
not or and	逻辑运算符
= += -= *= /= %= //= **= &= |= ^= >>= <<=	(复合)赋值运算符
说明: 在实际开发中,如果搞不清楚运算符的优先级,可以使用括号来确保运算的执行顺序。

The following example illustrates the use and assignment operator compound assignment operators.

"""
赋值运算符和复合赋值运算符

Version: 0.1
Author: 骆昊
"""

a = 10
b = 3
a += b # 相当于:a = a + b
a *= a + 2 # 相当于:a = a * (a + 2)
print(a) # 想想这里会输出什么

The following example demonstrates the comparison operators (relational operator), and the identity of the logical operators operators.

"""
比较、逻辑和算身份运算符的使用

Version: 0.1
Author: 骆昊
"""

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

Here Insert Picture Description

Here Insert Picture Description

Here Insert Picture Description

Published 124 original articles · won praise 141 · views 160 000 +

Guess you like

Origin blog.csdn.net/weixin_36838630/article/details/105202881