Start Python3 programming on Unix-like systems

Source: Internet
Author: User
This article mainly introduces how to start Python3 programming on Unix-like systems, and explains the most basic and intuitive method of using the Print function for various outputs, if you need a python script, refer to the following example. py, regardless of which Unix platform, can be executed through the interpreter on the command line:

$ python script.py

The Unix platform can also automatically execute the python interpreter without explicitly specifying the python interpreter. You need to add the following shell magic string to the first line of the python script:

#!/usr/bin/python

In #! Then write the complete path of the python interpreter, usually in the/usr/bin or/usr/local/bin directory. Another way is to use the env command in/bin or/usr/bin, which will help you find the python interpreter in the system search path, the first line of the python script can be modified as follows:

#!/usr/bin/env python

In this way, you do not need to explicitly call the python interpreter when executing the python script. You only need to enter the script file name:

$ script.py

In Python 3, the first major difference is that indentation is part of the syntax, which is indeed different from other languages such as C ++, so be careful.
Indentation requires four spaces (this is not necessary, but you 'd better do it). indentation indicates the beginning of a code block, and non-indent indicates the end of a code. There are no explicit braces, braces, or keywords. This means that the blank space is important and must be consistent. The first line without indentation marks a code block, which means the end of a function, if statement, for loop, while loop, and so on.
However, such a rule makes the Python program more beautiful and easy to read. It is useless to vomit. Accept it... o (begin □success) o
Python ideas:
"Everything is an object !"
The input is simple.

x = input("Please input x:") Please input x: 

Add

input("Press Enter") 

You can stop the program after it is run.

Summary of the output print function:
1. String and numeric type
Can be output directly

>>> print(1) 1 >>> print("Hello World") Hello World 

2. Variables
No matter what type, value, Boolean, list, dictionary... can be directly output

>>> x = 12 >>> print(x) 12 >>> s = 'Hello' >>> print(s) Hello >>> L = [1,2,'a'] >>> print(L) [1, 2, 'a'] >>> t = (1,2,'a') >>> print(t) (1, 2, 'a') >>> d = {'a':1, 'b':2} >>> print(d) {'a': 1, 'b': 2} 

3. format the output
Similar to printf in C

>>> s 'Hello' >>> x = len(s) >>> print("The length of %s is %d" % (s,x)) The length of Hello is 5 

Let's take a look at the summary of formatting output in basic Python programming:
(1). % character: mark the start of the conversion specifier

(2 ). conversion flag:-indicates the left alignment; + indicates adding a plus or minus sign before the conversion value; "" (blank character) indicates retaining spaces before a positive number; 0 indicates that the conversion value is filled with 0 if the number of digits is not enough.

(3). Minimum field width: The converted string must have at least the width specified by this value. If it is *, the width is read from the value tuples.

(4). Point (.) followed by precision value: If the conversion is a real number, the precision value indicates the number of digits after the decimal point. If the conversion is a string, the number indicates the maximum field width. If it is *, the precision will be read from the tuples.

(5). string format conversion type

Conversion Type meaning
D, I-Signed decimal integer
O unsigned octal
U unsigned decimal
X non-Signed hexadecimal (lower case)
X unsigned hexadecimal (uppercase)
E. Floating-point numbers represented by scientific notation (lower case)
E. floating point number represented by scientific notation (uppercase)
F, F decimal floating point number
G. If the index is greater than-4 or smaller than the precision value, it is the same as e. Otherwise, it is the same as f.
G. If the index is greater than-4 or smaller than the precision value, it is the same as E. Otherwise, it is the same as F.
C single character (accept integer or single character string)
R string (use repr to convert any python object)
S string (use str to convert any python object)

>>> Pi = 3.141592653 >>> print ('% 10.3f' % pi) # field width 10, precision 3 3.142 >>> print ("pi = %. * f "% (3, pi) # Use * to read the field width or precision from the following tuples. pi = 3.142 >>>> print ('% 010.3f' % pi) # Fill the blank with 0: 000003.142 >>> print ('%-10.3f' % pi) # align left: 3.142 >>>> print (' % + F' % pi) # Show plus and minus signs + 3.141593


4. How to keep print out of line breaks
In Python, line feed is always used by default.

>>> for x in range(0,10):  print(x) 0 1 2 3 4 5 6 7 8 9 

If you want to avoid line breaks, you can print x in the previous 2.x version and add it at the end,
But it does not play any role in 3.x.
To wrap a line, you should write it as print (x, end = '')

>>> for x in range(0,10):  print (x,end = '') 0123456789 


Concatenated string:

>>> "Hello""World" 'HelloWorld' >>> x = "Hello" >>> y = "world" >>> xy Traceback (most recent call last):  File "
 
  ", line 1, in 
  
     xy NameError: name 'xy' is not defined >>> x+y 'Helloworld' 
  
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.