day2
You bury the mud pin bone under the spring , I'm sending snow all over the world .
Language gossip
Tracing the roots
feng · Neumann structure
Nowadays, most computer hardware systems are still based on Feng · Neumann structure established .
feng · Neumann structure mainly has two aspects
- The storage device is separated from the central processing unit
- Data is encoded in binary
therefore , The hardware system of a computer is usually composed of five parts , Include : Arithmetic unit , controller , Memory , Input devices and output devices . among , The combination of arithmetic unit and controller is what we usually call a central processor , Its function is to execute various operation and control instructions and process data in computer software . What we call a program is a set of instructions , The program is to A series of instructions are combined in some way , Then use these instructions to control the computer to do what we want it to do .
Variables and types
Variables are essentially a piece of memory space of a computer , Generally, it has read and write operations . There are many kinds of data processed by computers , In addition to values , It also includes text , graphics , Audio , Video and other data .python There are many types of data in , Like any other language , Users can also customize new data types . Next in , First introduce some new partners .
- integer :python Can handle integer of any size ,(python2 There is
intandlongTwo types of integers distinguish , But this distinction lies in python3 Disappeared in , In the end, there are only integers leftintBig brother stayed , So when writing large integers plus, minus, multiply and divide in the future , Know what to use ), stay python In binary , octal , Decimal system , The hexadecimal representation is as follows ( It is unified here 16)
| Base number | Representation |
|---|---|
| Binary system | With 0b start ,0b10000 |
| octal | With 0o start ,0o20 |
| Decimal system | Just write it directly ,16 |
| Hexadecimal | With 0x start ,0x10 |
- floating-point : Floating point numbers are decimals , Relative to the fixed number , Fixed point number means that the decimal point is fixed , Floating point numbers are decimal points that are not fixed , Because the representation of floating-point numbers adopts scientific counting , Therefore, the position of the decimal point can be changed .
a = 1.234 # Fixed-point number
b = 12e34 # Floating point numbers
- String type : Strings are in single quotes or Double quotes or Any text enclosed in three quotation marks , such as
'abc',"abc",'''abc''',"""abc""", Of course, it also includes the original stringr'\n'( At this time, the output is not line feed , But the original text content \n, That is to say, the original content will be output , Instead of the content after escape ), Byte string ,Unicode If you are interested in strings, you can understand them by yourself . Note that only three quotation marks support multiline form , Others cannot . - Boolean type :python There are only
TrueandFalseTwo values . Pay attention to the case here ,python It's a case sensitive language . Of course, you can also assign values through expressions that return Boolean values , such as1 < 2perhaps1 == 2etc. . - Plural :
1 + 1j, This form is the representation of the plural , Note that the imaginary units here use j Rather than using i, Other expressions match the mathematical knowledge learned before .
Variable naming
Your name is the shortest love poem I have ever read
The naming of variables follows the following hard rules ( Give big brother interpreter a face ):
- Variable names are made up of letters ( The generalized Unicode character , Excluding special characters )、 Numbers and underscores , The number can't start
- Case sensitive ( uppercase A And lowercase a It's a different variable )
- Don't follow keywords ( Words with special meanings ) And system reserved words ( Such as function 、 Name of module, etc ) Conflict
Artificial requirements are as follows ( You can't abide by , Just suggest )
- Spell in lowercase , More than one word is underlined
- Protected instance properties start with a single underscore
- Private instance properties start with two underscores
Of course, now cooperative programming is the best , Therefore, it is also very important to know the meaning of variable naming .
Use of variables
python Different from other languages , There is no need to worry about not remembering variable types , It automatically matches variable types . That is, the definition and declaration of variables are direct a = 123 Is to define an integer variable a, There is no need to add int. therefore , If a = 1, b = 2, a/b stay python The output of is 0.5 It's not like C Medium 0.
a = 321
b = 123
print(a + b)
print(a - b)
print(a * b)
print(a / b)
stay python Can be used in type Function to check the type of the variable . The concept of function here is general , The author doesn't say .
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))
"""
output:
<class 'int'>
<class 'float'>
<class 'complex'>
<class 'str'>
<class 'bool'>
"""
Do type conversion , have access to python The built-in functions in convert variable types . Pay attention to python The variable type in is not what we define , So if it is
a = 123
b = 12.3
a = b
It's not like C Cast in , It's about putting a Also become 12.3, therefore python It is recommended to use python Built in type conversion function , Otherwise, it is easy to make some strange mistakes .
| function | effect |
|---|---|
int() | Convert a numeric value or string to an integer , You can specify base ( Here, the string specifies what base is converted to decimal ) |
float() | Convert a string to a floating point number |
str() | Converts the specified object to string form , You can specify the code |
chr() | Convert the integer to the string corresponding to the encoding ( A character ) |
ord() | The string ( A character ) Convert to the corresponding code ( Integers ) |
bool() | Convert to Boolean |
complex() | Convert to plural type |
Here comes the stuffed duck :input() It means reading data from the keyboard , String can be filled in brackets , Indicates output to the output device , As a reminder
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))
Be careful : notice input() You'll know python Preferential treatment for strings . The first is to convert the information entered on the keyboard into string form for storage , If there is no type conversion , So the default is str type , So use input Generally, you need to use the preceding type conversion function . The placeholder in front of this side , and C/C++ Similar , No more details here . Be careful python Format of the formatted output statement in , It's through % To segment the , No ,, Commas are used to separate statements , The data needed to occupy the space is written in parentheses , Separate by comma .
ps: there // yes python The product of division and division ,// For division ,/ Represents normal division ,** Represents a power operation .
Operator
The commonplace operator priority table of the great demon level ( Can we use brackets to distinguish priorities these days )
| Operator | describe |
|---|---|
[],[:] | Subscript , section |
** | Index |
~,+,- | According to the not , The sign |
*,/,%,// | ride , except , model , to be divisible by |
+,- | Add , reduce |
>>,<< | Move right , Move left |
& | Bitwise AND |
^,| | Bitwise XOR , Press bit or |
<=,<,>,>= | Less than or equal to , Less than , Greater than , Greater than or equal to |
==,!= | be equal to , It's not equal to |
is,is not | Identity operator |
in,not in | member operator |
not,or,and | Logical operators |
=,+=,-=,*=,/=,%=,//=,**=,&=,|=,^=,>>=,<<= | Various assignment operators |
It is suggested to use parentheses to ensure the execution order of operations
Assignment operator
Is to assign the value of the right to the left , Also distinguish = and == The beginning of
a = 10
b = 3
a += b
a *= a + 2
print(a)
a *= a + 2 because + Has a higher priority than *=, Can't , The assignment operator has the lowest priority , The most humble . So this statement is actually equivalent to a = a * (a + 2)
Comparison operators and logical operators
The value of the expression composed of comparison operators is Boolean , Include ==,!=,<,<=,>,>=, Note that it's not mathematical to judge whether it's equal here =, It is ==.
There are three logical operators ,and,or and not.
andIs and operation , Only the left and right Boolean values are True The expression will return True, Otherwise return to False. IfandThe Boolean on the left is False When , So no matter what value is on the right , The final result is False, Therefore, the value on the right will be skipped when doing the operation ( Short circuit treatment ), namelyandThe value on the left is False When , The expression on the right does not execute at all , This and C/C++ Very similar .orOr operation , Only the left and right Boolean values are False When , The final result will be False, Otherwise, it's all for True.orThere is also short-circuit operation , As long as the Boolean value on the left is True, Then the expression on the right will not execute at all .notIs non operation . It just adds a Boolean value after it , That is, unary operator , The function is to return the Boolean value opposite to the following Boolean value , For example, the back is True, Return is False.
a = 1==1
b = 3 > 2
c = 2 < 1
d = a and c
e = a or c
f = not(1 != 2)
print(a)
print(b)
print(c)
print(d)
print(e)
print(f)
'''
output:
True
True
False
False
True
False
'''
Practice after class
- here
%.1fand C/C++ The placeholder in has the same meaning , Also output decimals , Keep one decimal place - About
print(f'{f:.1f}')Explanation of output : Add... Before the string f Represents a formatted string , That is, at this time, our original placeholder can be replaced with braces , The area surrounded by braces is the area to be replaced . - Writing code across lines can help
()You can also use '\', But for\\There must be an operator in front, otherwise it will report an error or get the wrong answer , Because at this time, the system does not think that you are writing expressions across lines
About str Type of supplement
| function | function |
|---|---|
len() | Gets the length of the string ,len(str) |
title() | Capitalize the first letter of each word in the string ,str.title() |
upper() | Turn all the letters in the string into uppercase ,str.upper() |
lower() | Change all the letters in the string to lowercase ,str.lower() |
startwith(str1) | Is the string in str1 At the beginning ,str.startwith(str1) |
endwith(str1) | Is the string in str1 At the end of the ,str.endwith(str1) |
Here, strings and strings can be spliced directly , namely str1 + str2 That's all right. , If str * int, So that is str repeat int All over

![[OGeek2019]babyrop](/img/7a/18e8b985629488346e596cdf2a215c.png)






