Instructions and procedures
The hardware system of a computer usually consists of five major components, including: an operator, a controller, a memory, an input device and an output device. Among them, the operator and controller together is what we usually call the central processor, its function is to execute various operation and control instructions as well as to process the data in the computer software. What we usually call a program is actually a collection of instructions, our program is a series of instructions organized in a certain way, and then through these instructions to control the computer to do what we want it to do. Although the computers we use today are becoming more and more sophisticated, with more and more powerful processing power, they still belong to the "von Neumann structure" of computers in essence. "There are two key points in the Von Neumann structure, one is the separation of the storage device from the central processor, and the other is the proposal to encode data in binary. Binary is a kind of "one in two" counting method, which is not substantially different from the "one in ten" counting method used by human beings, who use decimal system because they have ten fingers (because they can only round up when they run out of fingers), but of course there are exceptions. Of course, there are exceptions, the Mayans may be barefoot for many years because of the reason that the toes are also counted, so they used the binary counting method, in this counting method under the guidance of the Mayan calendar is not the same as our usual calendar, and according to the Mayan calendar, 2012 is the last year of the last so-called "Solar Era", and the Mayan calendar, 2012 is the last year of the so-called "Solar Era". According to the Mayan calendar, 2012 is the last year of the last so-called "solar era", and 2013 is the beginning of the new "solar era", and this matter was later misrepresented as "2012 is the end of the world as predicted by the Mayans", which is an absurd statement. (Today we can venture to guess that the reason for the slow development of the Mayan civilization is also related to the use of binary). For computers, binary is the easiest to implement in terms of physical devices (high voltage means 1, low voltage means 0), so all computers in the "von Neumann structure" use binary. While we don't need every programmer to be able to work with a binary mindset, it is necessary to understand binary and how it relates to the decimal system we live in, as well as how binary relates to octal and hexadecimal. If you are not familiar with this, you can use Wikipedia or Baidu Encyclopedia to popularize it yourself.
Variables and types
- In programming, a variable is a vehicle for storing data. A variable in a computer is the actual data or a piece of memory space in which the data is stored, the value of the variable can be read and modified, which is the basis of all calculations and control. There are many types of data that computers can handle, in addition to numeric values can also handle text, graphics, audio, video, and other kinds of data, so different data will need to define different types of storage.Python has a lot of data types, and also allows us to customize the new data types (which will be talked about later), let's start with the introduction of a few commonly used data types.
- Integer: Python can handle integers of any size (there are two types of integers in Python, int and long, but this distinction does not make much sense for Python, so there is only one type of integer in Python, int), and support for binary (e.g., 0b100, which translates to 4 decimal), octal (e.g., 0o100, which translates to 64 decimal), decimal (100), and hexadecimal (0x100, which translates to 256 decimal) representations. is 64), decimal (100), and hexadecimal (0x100, which converts to decimal is 256) representations.
- Floating Point: Floating point numbers are also known as decimals, the reason why they are called floating point numbers is because when expressed in scientific notation, the decimal point position of a floating point number is variable, floating point numbers in addition to mathematical writing (e.g., 123.456) in addition to support for scientific notation (e.g., 1.23456e2).
- String type: String is a single or double quotes to enclose any text, such as 'hello' and "hello", string and the original string representation, byte string representation, Unicode string representation, and can be written in the form of multiple lines (with three single quotes or three double quotes at the beginning of the three single quotes or three double quotes at the end).
- Boolean: Boolean value has only two values True, False, either True or False, in Python, you can directly use True, False to represent the Boolean value (please pay attention to the case), but also through the Boolean operation can be calculated (for example, 3 < 5 will produce a Boolean value True, while 2 == 1 will produce a Boolean value False).
- Complex type: shaped like 3+5j, it's the same as the mathematical representation of a complex number, the only difference being that the i in the imaginary part is replaced with a j.
variable naming
For each variable we need to give it a name, just as each of us has our own ringing name. In Python, variable naming needs to follow these mandatory hard rules and highly recommended non-hard rules.
Hard and fast rules:
- Variable names consist of letters (broad Unicode characters, excluding special characters), numbers, and underscores, and cannot begin with a number.
- Case sensitive (upper case a and lower case A are two different variables).
- Do not conflict with keywords (words with special meanings, covered later) and system reserved words (such as the names of functions, modules, etc.).
PEP 8 requirements:
- Spell in lowercase letters and connect multiple words with underscores.
- Protected instance properties begin with a single underscore (more on this later).
- Private instance properties begin with two underscores (more on this later).
Of course, as a professional programmer, it is also very important to name variables (and indeed all identifiers) in such a way that you know what you are talking about.
Use of variables
The following are a few examples to illustrate variable types and variable usage.
""" Using variables to store data and perform arithmetic operations Version: 0.1 Author: Luo Hao """ 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) """ utilizationinputfunction input utilizationint()perform type conversion Formatting the output string with placeholders Version: 0.1 Author: Luo Hao """ 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)) """ utilizationtype()Checking the type of a variable Version: 0.1 Author: Luo Hao Date: 2018-02-27 """ 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))
You can use Python's built-in functions (to be precise, the ones listed below are not really functions, but constructor methods for creating objects, which we'll talk about later) when converting variable types.
- int(): Converts a value or string to an integer, and can specify the system.
- float(): Convert a string to a floating point number.
- str(): convert the specified object to string form, can specify the encoding.
- chr(): converts an integer to a string (one character) corresponding to that code.
- ord(): convert a string (a character) to the corresponding code (an integer).
operator (computing)
Python supports a variety of operators, and the following table lists all of them in roughly descending order of priority, and we'll use them one by one.
operator (computing) | descriptive |
---|---|
[] [:]
|
Subscripts, Slices |
** |
exponents |
~ + -
|
Bitwise inverse, plus or minus sign |
* / % //
|
Multiply, Divide, Modulo, Integer Division |
+ -
|
add, subtract |
>> <<
|
Move right, move left |
& |
compatibility with |
^ ` |
` |
<= < > >=
|
Less than, equal to, less than, greater than, greater than or equal to |
== !=
|
Equal to, not equal to |
is is not
|
identity operator (computing) |
in not in
|
membership operator |
not or and
|
logical operator |
= += -= *= /= %= //= **= &= ` |
= ^= >>= <<=` |
**Note:** In practice, if you can't figure out the priority you can use parentheses to ensure that the operations are executed in the right order.
The following example demonstrates the use of operators.
""" Use of operators Version: 0.1 Author: Luo Hao """ 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)
practice
Exercise 1: Fahrenheit to Celsius.
""" Converting Fahrenheit to Celsius F = 1.8C + 32 Version: 0.1 Author: Luo Hao """ f = float(input('Please enter the temperature in Fahrenheit: ')) c = (f - 32) / 1.8 print('%.1fdegrees Fahrenheit = %.1fdegrees centigrade' % (f, c))
Exercise 2: Calculate the perimeter and area by entering the radius of a circle.
""" Calculate the circumference and area of a circle by entering the radius Version: 0.1 Author: Luo Hao """ import math radius = float(input('Please enter the radius of the circle: ')) perimeter = 2 * * radius area = * radius * radius print('Perimeter: %.2f' % perimeter) print('Area: %.2f' % area)
Exercise 3: Enter the year to determine if it is a leap year.
Enter the year If it is a leap year outputTrue Otherwise outputFalse Version: 0.1 Author: Luo Hao """ year = int(input('Please enter the year: ')) # If the code is too long to read on one line, you can break it up with \ or (). is_leap = (year % 4 == 0 and year % 100 != 0 or year % 400 == 0) print(is_leap)