Getting Started with Python (ii)--ide select Pycharm, input and output, basic specifications, data types and variables, constants, strings and encodings, formatting
We start from today to formally learn the PY transaction, the PY transaction is OK, we have the foundation after learning not to say so passive, I study is Python2.7, although now with the version of the migration to Python3, but this does not cause any trouble to us, after all, is just an adaptation stage
Our study materials: official website of Liao Xuefeng Python2.7 tutorial
One. IDE Select Pycharm
Although we have been configured in the previous chapter, but we still have a lot of IDE can develop py, such as sublime text, such as notpad++, in fact, is all right, but you want to find a special for development, such as Android Studio level of development tools , then I recommend pycharm here without doubt, and use it directly.
Installation of the process everyone on their own Baidu, as well as the configuration of what things, and this is based on idea to achieve, all people should pro
Two. Input and output
Output I believe we should be very familiar with, here I list a few output, we will see a
print"Hello World!"print"Hello""World!"print100200print100200print100200
The result of this output can be seen
OK, output nothing to say, we look at the input, the input of the fact that the PY has a specific function, that is raw_input (), we look at the actual case
name = raw_input("Please enter your name \n""your name : ",name
The meaning of this code is obvious, is prompted to enter the name, and then I enter the name after printing out, to see the run
Any computer program is to perform a specific task, with the input, the user can tell the computer program required information, with the output, the program can be run to tell the user the results of the task.
Inputs are input and outputs are output, so we refer to the input and output collectively as input/output, or abbreviated to IO.
Raw_input and print are the most basic inputs and outputs below the command line, but the user can also complete the input and output through other more advanced graphical interfaces, such as entering their name in a text box on a webpage and clicking "OK" to see the output on the page. These are all something.
Three. Basic specifications
Let's take a look at the official example.
# print absolute value of an integer:a100ifa0: aelse: print -a
This code is very simple, a comment, a judgment statement, in the PY, the comment is the beginning of the #
Each of the other lines is a statement that, when the statement ends with a colon ":", the indented statement is treated as a block of code.
Indentation has pros and cons. The advantage is that you are forced to write the formatted code, but there is no rule that indents are a few spaces or tabs. In accordance with established management, you should always stick to the indentation of 4 spaces.
Another benefit of indenting is forcing you to write less indented code, and you tend to split a very long piece of code into several functions, resulting in less code being indented.
The downside of indentation is that the "copy-paste" function fails, which is the most pit-daddy place. When you refactor the code, the pasted past code must re-check that the indentation is correct. In addition, it is difficult for the IDE to format Python code just like formatted Java code.
Finally, it is important to note that the Python program is case-sensitive, and if the case is written incorrectly, the program will give an error.
Four. Data types and variables, constants
Well, this is almost like Java, and we analyze it.
Python can handle any number of integers, also including the secondary, 1,100, -20, and so on, the computer from the binary sometimes hexadecimal representation of integers more convenient, hexadecimal using 0x prefixes and 0-9,a-f, such as 0xff00,0xdf522, etc.
Floating-point numbers are also decimal points, which is called floating-point numbers because the point position of a floating-point number is variable when the scientific notation is represented, such as 1.23x109 and 12.3x108 are equal. Floating-point numbers can be written in mathematical notation, such as 1.23,3.14,-9.01
Integers and floating-point numbers are stored inside the computer in different ways, and integer operations are always accurate (is division accurate?). Yes! ), and the floating-point operation may have rounding errors.
strings are either single or double quotes, such as "ABC" or "ABC"
If the string contains "and contains" inside, you can use the escape character \ To identify it, such as
print"I\‘m OK!"
This output
There are many escape characters, such as \ n identifies the newline, \ t identifies the tab, \ is itself escaped, and all \ Identity characters are \, so it can be seen in Python's interactive commands.
print"Im\OK"print"Im\\OK"print"Im\nOK"print"Im\tOK"
Let's take a look at the output
You can see \ and \ are the same, \ n newline, \ t is a space
If there are many characters in the string that need to be escaped, you need to add a lot of \, in order to simplify, Python also allows the use of R "to express" internal strings are not escaped by default, we look at the code
print‘\\\t\\‘printr‘\\\t\\‘
In this case, we output:
As you can see, the first is a double slash and then a tab and then a double slash, so the output \ \, and we added R is not escaped, that is, all output
If there is a lot of line wrapping inside the string, written in a row is not good to read, in order to simplify, Python allows the "' ..." format to represent multiple lines of content, we look at the code
print‘‘‘line1 line2 line3‘‘‘
Look at the output.
In fact, this is very good understanding, is the front and back are three points to show, and then the middle of the random line, anyway, it will be formatted on the
The Boolean value is two, true and false, and the difference between him and Java is that he is capitalized in the first letter. Let's see.
print100200print100200
This will output the
Here's the Boolean value, so let's talk about his calculations.
The and operation is associated with an operation, and only the result of all True,and operations is true:
Let's take a look at the example
printTrueandTrueprintTrueandFalseprintFalseandFalse
Here we are not hard to guess, the results of his calculations
An OR operation is an operation, as long as one of the true,or operation results is true:
Let's take a look at the example
printTrueorTrueprintTrueorFalseprintFalseorFalse
What is the result of the operation?
The not operation is a non-operation, which is a single-mesh operator that turns true into False,false true:
Let's take a look at the example
printnotTrueprintnotFalse
You can understand him as a counter-offer.
The null value is a special value in Python, denoted by none. None cannot be understood as 0, because 0 is meaningful, and none is a special null value.
You can interpret him as Null in Java.
Variables are hypothetical, such as we have a math problem, knowing that the perimeter of this square is 12, then we can calculate
a * 4 = 12
And this a is the variable
The variable name must be a combination of uppercase and lowercase English, numeric, and _, and cannot start with a number, such as:
a = 1
This means that the variable A is an integer
In Python, equals = is an assignment statement that assigns any data type to a variable, the same variable can be repeatedly assigned, and can be a variable of different types, for example:
# a是整数a123a# a变为字符串a‘ABC‘a
This type of variable itself is called Dynamic language, which corresponds to static language. Static languages must specify the variable type when defining the variable, and if the type does not match, the error will be
Finally, it is important to understand the representation of variables in computer memory. When we write:
a‘ABC‘
This time, Python actually did two things.
- 1. Create a string of ' ABC ' in memory
- 2. Create a variable named a, and point him to ' ABC '
You can also assign a variable to another variable, which is actually referring to the string in front of the variable pointed to the following variables, such as:
a"ABC"a;print b
This is undoubtedly input ABC
So-called variables are variable quantities, constants are constant, such as π, in Python, usually all uppercase variable names represent constants, such as
PI5555PI
In fact, this pi is still a variable, Python does not have any mechanism to ensure that PI will not be changed, so, with all uppercase variable name to indicate that the constant is only a customary usage, if you must change the value of the variable pi, no one can stop you, haha.
Five. Strings and encodings
In Python, Python provides the Ord () and Chr () functions to convert letters and corresponding numbers to each other:
printord(‘A‘)printchr(65)
Let's see what the output is.
This is Python's support for ASCII encoding, and Python has added support for Unicode later, and the Unicode-represented string is denoted by U ' ... ', for example:
printu‘刘桂林‘
If you need to transcode, you can do this.
printu‘ABC‘.encode(‘utf-8‘)printu‘刘桂林‘.encode(‘utf-8‘)
If the anti-code, you can use decode
If you want to see the length of a string, Python has a Len function that supports
print len("ABC")print len(u"刘某人程序员")
Can see
In fact, the way to solve the coding is very simple, we just need to specify the code is good, in the first line plus
#coding:utf-8
This will support Chinese.
Six. Formatting
What are we going to output some fixed-format statements, such as
亲爱的xx你好,今天是xxx,你还剩下xx天还款
This xx is a change, in Python, with% to format the statement, such as:
print‘亲爱的%s,你好,今天是%s,你还剩下%d天还款‘ % (‘刘桂林‘‘2017年4月27日‘3)
Let's look at the output first.
Is it good to understand that this type of escape character substitution, followed by% to pick up a slogan, in parentheses corresponds to the content to be filled
Common placeholders are:
- %d integers
- %f floating Point
- %s string
- %x hexadecimal integer
If you're not sure what it is,%s will always work.
In fact, the so-called code, or Python version of the problem, after we learn in depth when the natural familiar with, well, today this chapter ends here, we will be more and more in-depth to learn python, everyone together refueling it!
Getting Started with Python (ii)--ide select Pycharm, input and output, basic specifications, data types and variables, constants, strings and encodings, formatting