In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces you how to Python the variables in the language elements, the content is very detailed, interested friends can refer to, hope to be helpful to you.
As a programmer, you may often be asked two questions by laymen, one is "what is a (computer) program" and the other is "what a (computer) program can do". Here we first give an answer to two questions. A program is a collection of instructions, and writing a program is to use instructions to control the computer to do what we want it to do. So why write programs in Python? Because Python is simple and elegant, Python is more friendly to beginners than programming languages like C, C++ and Java. Of course, this is not to say that Python is not as powerful as other languages. Python is almost omnipotent. In the first class, we mentioned that Python can be used in server program development, cloud platform development, data analysis, machine learning and other fields. Of course, the Python language can also be used to glue systems developed in other languages, so it is often jokingly called the "glue language".
Some computer common sense
Before we begin to learn programming systematically, let's popularize some basic knowledge of computers. The hardware system of a computer is usually composed of five components, including an arithmetic unit, a controller, a memory, an input device and an output device. Among them, the arithmetic unit and the controller together is what we often call the central processing unit, its function is to perform a variety of operations and control instructions. We just mentioned that a program is a collection of instructions. Writing a program is to organize a series of instructions together in a certain way, and then use these instructions to control the computer to do what we want it to do. At present, the computers we use are basically "von Neumann architecture" computers, which have two key points: one is to separate the storage device from the CPU; the other is to encode the data in a binary way.
Binary is a counting method of "one for every two", which is essentially the same as the "one for every decimal" used by human beings. Humans use decimal because they have ten fingers, because when ten fingers are used up, they can only be represented by rounding. Of course, there are exceptions to everything. The Mayans may have used their toes because they had been barefoot for years, so they used the decimal counting method. Under the guidance of this counting method, the Mayan calendar is different from the calendar we usually use. According to the Mayan calendar, 2012 was the last year of the last so-called "solar age", while 2013 was the beginning of a new "solar age", which was later misinterpreted as the myth that "2012 was the end of the world predicted by the Mayans." Today, many people are speculating that the slow development of Mayan civilization has something to do with the use of decimal. For computers, binary is the easiest to implement on physical devices, because 1 can be represented by high voltage and 0 by low voltage. Not all programmers need to know how to convert decimal to binary. Most of the time we can write programs even if we don't know this knowledge, but we have to know that computers use binary counting. no matter what data enters the computer memory, it exists in binary form.
Variables and types
If you want to keep data in computer memory, you must first talk about the concept of variables. In programming language, variable is the carrier of data, which is simply a piece of memory space used to store data, and the value of variable can be read and modified, which is the basis of all calculation and control. There are many types of data that computers can process, the most common of which is numerical values. In addition to numerical values, there are all kinds of data, such as text, graphics, audio, video and so on. Although data exists in binary form in computers, we can use different types of variables to represent differences in data types. There are many data types in Python, and they also allow us to customize new data types (which we'll talk about later). Here we need to learn about several commonly used data types.
Integer (int): Python can handle integers of any size and supports representations of binary (such as 0b100, converted to decimal 4), octal (such as 0o100, converted to decimal 64), decimal (100), and hexadecimal (0x100).
Floating point (float): floating-point numbers, that is, decimals, are called floating-point numbers because the position of the decimal point of a floating-point number is variable when expressed according to scientific notation, and floating-point numbers support scientific counting in addition to mathematical writing (e.g. 123.456).
Str: a string is any text enclosed in single or double quotation marks, such as' hello' 'and' hello'.
Bool: there are only two Boolean values, True and False, either True or False.
Variable naming
We need to give each variable a name, just as each of us has our own name. In Python, variable naming needs to follow the following rules, which are divided into hard rules that must be followed and non-hard rules that are recommended.
Hard rules:
Variable names consist of letters, numbers, and underscores, and numbers cannot begin. It should be noted that the letters here refer to Unicode characters, Unicode known as universal codes, including most of the text systems in the world, which means that Chinese, Japanese, Greek letters and so on can be used as characters in variable names, but special characters such as!, @, # cannot appear in variable names, and we strongly recommend that you use English letters as much as possible.
Case-sensitive, in short, uppercase An and lowercase an are two different variables.
Variable names should not conflict with Python keywords (words with special meanings, which will be discussed later) and reserved words (such as the names of functions, modules, etc.).
Non-rigid rules:
Variable names are usually in lowercase letters, and multiple words are concatenated with underscores.
Protected variables begin with a single underscore (more on that later).
Private variables begin with two underscores (more on that later).
Of course, as a professional programmer, it is also important to name variables (which should in fact be all identifiers).
The use of variables
Here are a few examples to illustrate the type and use of variables.
"
Use variables to save data and add, subtract, multiply and divide
Version: 0.1
Author: Luo Hao
"
A = 45 # variable a saved 45
B = 12 # variable b saved 12
Print (a + b) # 57
Print (a-b) # 33
Print (a * b) # 540
Print (a / b) # 3.75
You can use the type function in Python to check the type of a variable. The concept of function in programming is consistent with the concept of function in mathematics. I believe that the function in mathematics is no stranger to us. It includes function name, independent variable and dependent variable. It doesn't matter if you don't understand this concept for the time being, we will focus on the definition and use of functions in the following content.
"
Use type () to check the type of variable
Version: 0.1
Author: Luo Hao
"
A = 100
B = 12.345
C = 'hello, world'
D = True
Print (type (a)) #
Print (type (b)) #
Print (type (c)) #
Print (type (d)) #
Different types of variables can be converted to each other, which can be achieved through Python's built-in functions.
Int (): converts a numeric value or string to an integer, which can be specified as a base.
Float (): converts a string to a floating point number.
Str (): converts the specified object to a string, and you can specify the encoding.
Chr (): converts an integer to a string (one character) corresponding to the encoding.
Ord (): converts a string (one character) to the corresponding encoding (integer).
The following example demonstrates type conversion in Python.
"
Type conversion in Python
Version: 0.1
Author: Luo Hao
"
A = 100
B = 12.345
C = 'hello, world'
D = True
# convert integers to floating-point numbers
Print (float (a)) # 100.0
# floating-point conversion to string (you won't see quotation marks when outputting a string)
Print (str (b)) # 12.345
# string is converted to Boolean (strings with content will become True)
Print (bool (c)) # True
# Boolean to integer (True will be converted to 1)
Print (int (d)) # 1
# change the integer to the corresponding character (97 coincides with the letter an in the character table)
Print (chr (97)) # a
# convert characters to integers (characters and string representations are the same in Python)
Print (ord ('a')) # 97
In the Python program, we can use variables to save data, variables have different types, variables can do operations, variables can also be type-converted.
On how to share the variables in the Python language elements here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.