The first Python program

Source: Internet
Author: User

Probably every programmer starts with Hello World, so let's start our Python journey from Hello World!

Open Pycharm, create a new project, under Project New file, enter the following:

Print (' Hello World ')

Then hold down the SHIFT+F10 and you'll see the output below

Hello World

Our Python tour has officially begun.


Let's talk about some basic Python syntax.

Coding:

By default, Python 3 source files are encoded in UTF-8, and all strings are Unicode strings. Of course, we can also set the encoding format ourselves

#-*-coding:cp-1252-*-

It is important to note here that those who like to write code with the Windows own notebook, try not to use Notepad, you can choose to use Notepad instead of Windows to bring Notepad, or you write a program may be wrong, or some strange things happen, the self-brain repair. Because Python uses Unicode encoding instead of ASSIC encoding.

Identifier:

The identifier for the Python language, like other languages, meets the following criteria

1. The first character must be a letter in the alphabet or an underscore ' _ '.

2. The other parts of the identifier are composed of letters, numbers, and underscores.

3. Identifiers are sensitive to case

Reserved words:

Reserved words are keywords, they cannot be used as identifiers, Python provides us with a keyword module that can output the keyword:

>>> import keyword>>> keyword.kwlist[' False ', ' None ', ' True ', ' and ', ' as ', ' assert ', ' Break ', ' class ', ' Continue ', ' Def ', ' del ', ' elif ', ' Else ', ' except ', ' finally ', ' for ', ' from ', ' global ', ' if ', ' import ', '-', ' is ', ' LAMBD A ', ' nonlocal ', ' not ', ' or ', ' Pass ', ' raise ', ' return ', ' Try ', ' when ', ' with ', ' yield ']>>>

Comments:

Python's comments are somewhat similar to the shell, with a single line comment starting with #, as follows:

#!/usr/bin/python3# First note print (' Hello World ') #第二个注释

We execute the above code, and we find that only Hello world is output.

For multiline comments, you can also use the format of "" to write longer notes between the three quotation marks

Line and Tighten:

Personally, this is a unique Python place, no longer using {} to represent blocks of code, but rather to be indented.

The number of locked spaces is variable, but the statement of the same code block must contain the same number of indented spaces. As shown in the following example:

Flag = Falseif Flag:print (' true ') Else:print (' false ') if ~flag:print (' true ') Else:print (' false ')

If the indentation is inconsistent, it can cause a run error

Flag = Falseif Flag:print (' true ') Else:print (' false ') if ~flag:print (' true ') Else:print (' false ')

The error prompts are as follows:

L:\mycode\python\learn\venv\Scripts\python.exe l:/mycode/python/learn/day1/if_else File "l:/mycode/python/learn/ Day1/if_else ", line ten print (' false ') ^indentationerror:expected an indented block


The first Python program

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.