Python-hello world!

Source: Internet
Author: User
Tags comparison table

First, Python installs Windows1, downloads the installation package
https://www.python.org/downloads/
2. Installation
Default installation path: C:\python3.5
3. Configure Environment variables:
"Right-click Computer"-"Properties"-"Advanced system Settings"-"Advanced"-"Environment variable"-"in the second content box to find the variable named path of a row, double-click"- "Python installation directory appended to the variable value, and split" such as: the original value ; C:\python3. 5, remember that there is a semicolon in front.
Linux, Mac

No installation required, original Python environment

  

Second, Hello World procedure

Create a file under Linux called hello.py, and enter

Print ("Hello world! ")

Then execute the command: Python hello.py, output

localhost:~ jieli$ vim hello.pylocalhost:~ jieli$ python hello.pyhello world!

Specify interpreter

When executing Python hello.py in the previous step, it is clear that the hello.py script is executed by the Python interpreter.

If you want to execute a python script like executing a shell script, for example:

Then you need to specify the interpreter at the head of the hello.py file, as follows:

# !/usr/bin/env python Print " Hello,world "

As a result, execute:./hello.py.

PS: Need to give hello.py execute permission before execution, chmod 755 hello.py

Executing in the interactive device

In addition to writing the program in the file, you can also call the Python's own interactive running code,

localhost:~jieli$ Pythonpython2.7.10 (default, Oct 23 2015, 18:05:06) [GCC4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on Darwintype" Help","Copyright","credits" or "License"  forMore information.>>>Print("Hello world!") Hello world!

These are the first programs that we wrote using Python:

print("Hello World")
Three, variable:

There are some data in the program that need to be reused several times, in order to facilitate writing, save time, Python provides one is to store the program in the process of some intermediate results, in order to facilitate later invocation.

Naming rules:

1, variable name can only _, number, letter composition, can not be a space or special characters (#?<.,¥$*!~)

2. Cannot use Chinese as variable name

3. Cannot start with a number

4. Variable names are case-sensitive

5, to have a descriptive

6. Reserved characters cannot be used

[' and',' as','assert',' Break','class','Continue','def','del','elif','Else','except','exec','finally',' for',' from','Global','if','Import','inch',' is','Lambda',' not','or','Pass','Print','Raise','return','Try',' while',' with','yield']
Four, if...else statement expression: The first assignment to the variable, if (if) the expression conforms to the variable, the operation required. else (otherwise) performs another operation. For example:
 No.ofclass1 = #    No.ofclass2  = #   variable assignment  if  No.ofclass1 < no.ofclass2: #   If statement condition  print  ( " yes  ) #   else  :  print  ( no   ") #   

The above simple if...else statement expression.

In addition to the IF...ELSE statement there are nested formula sentence: Elif. For example, guessing a number game:

num = into (input (">>>:"))#variable assignment is the number entered in interactive mode    ifnum = = 25:#If statement condition      Print("Yes")#when the action is satisfied    elifNum < 25:Print("smaller")#action when the second condition is met    Else:Print("bigger")#when not satisfied with the operation
Five, while loop expression: format: While condition:

When the condition is met, it will always circulate. This leads to the variable self-increment/decrement operator: the variable + = x/variable-= x, and the flag bit. While loop example:

# assigning values to variables  while # condition of the while loop Print ("*"# The increment operation of the action  # variable that satisfies the while loop      

The above simple while loop expression.

Of course, while loop statements can also be nested to meet more requirements:

num = 1#assigning values to variables     whileNum <= 5:#condition of the while loopnum2= num#assigning values to nested variables       whilenum2 <= Num:#conditions for nesting while loops        Print(" "* (5-num) +"*"* (num * 2-1), end ="")#operations that satisfy nested while loopsnum2+ = 1#self-increment operation of nested loop variables      Print()#operations that satisfy while loopsNum+ = 1#self-increment operation of cyclic variables

This way we will get a triangle composed of "*".

The break Interrupt Loop command is also in the while and continue jumps out of the loop command.

Vi. Interpretation of nouns

1, constants: Constant amount, such as Pie = 3.141592653 .... All variables in Python are mutable, so the variable name in all capitals represents the secondary variable as a constant.

2, character encoding: In order to let people interact with the computer A comparison table: Assic code table, UNICODE universal code. Currently, UTF-8 = Unicode extension set, variable long character encoding set is generally used.

3, note: In order to let the user or our own understanding of the process of writing. A single-line comment adds "#" in front of the bank. Multiline comment with three single quotes or three double quotation marks ' ' annotated content '. Multi-line annotations can also be used to represent multiple lines of printing. Cases:

Print ("'Hello World! ")

4, user interaction: input, can let the user input content, and return a user input.

5. Indent: Some encoding expressions have code ratings and need to use indentation notation.

Seven, the symbol
1, Operator: + 、-、、/, =, *,//,% 2, comparator:<, >, <=, >=,! =, = = 3, increment (decrement): + =,-+, =,/=, *=,//=,%= 4, logical operator: not,  and,or

Python-hello world!

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.