Python 03-python3 Basic Syntax

Source: Internet
Author: User

coding
  • By default, Python 3 source files are encoded in UTF-8
  • All strings are Unicode strings
  • You can specify a different encoding for the source file:
# -*- coding: cp-1252 -*-
identifiers
  • The first character must be a letter in the alphabet or an underscore _.
  • The other parts of the identifier are made up of letters, numbers, and underscores.
  • Identifiers are case sensitive.
  • In Python 3, non-ASCII identifiers are also allowed.
Python reserved word
  • Reserved words are keywords, we can't use them as any identifier names
  • Python's standard library provides a keyword module that can output all the keywords for the current version:
>>> import keyword>>> keyword.kwlist[‘False‘, ‘None‘, ‘True‘, ‘and‘, ‘as‘, ‘assert‘, ‘break‘, ‘class‘, ‘continue‘, ‘def‘, ‘del‘, ‘elif‘, ‘else‘, ‘except‘, ‘finally‘, ‘for‘, ‘from‘, ‘global‘, ‘if‘,‘import‘, ‘in‘, ‘is‘, ‘lambda‘, ‘nonlocal‘, ‘not‘, ‘or‘, ‘pass‘, ‘raise‘, ‘return‘, ‘try‘, ‘while‘, ‘with‘, ‘yield‘]
notes
  • Single-line comments in Python start with #
  • Multiline comments can be in multiple # numbers, plus "and" ":
#!/usr/bin/python3 # 第一个注释# 第二个注释 ‘‘‘第三注释第四注释‘‘‘ """第五注释第六注释"""print ("Hello, Python!")
Line and indent
  • Python's most distinctive feature is the use of indentation to represent blocks of code, without the need to use curly braces {}
  • The number of spaces to indent is variable, but the statement of the same block must contain the same number of indent spaces (typically 4 spaces)
if True:    print ("True")else:    print ("False")
Multiline Statements
  • Python usually writes a single statement on one line, but if the statement is long, we can use a backslash (\) to implement a multiline statement
  total = Item_one + Item_two + item_three  
  • Multi-line statements in [], {}, or () do not need to use backslashes ()
  tot Al = [' Item_one ', ' item_two ', ' item_three ', ' item_four ', ' item_five ']  
The variables in the
base data type
  • Python do not need to be declared. Each variable must be assigned before it is used, and the variable will not be created until the variable is assigned.
  • in Python, a variable is a variable, it has no type, and what we call "type" is the type of object in memory that the variable refers to. The
  • equals sign (=) is used to assign a value to a variable. The
  • equals sign (=) operator is on the left side of a variable name, and the right side of the Equals (=) operator is the value stored in the variable.
  counter = 100 # integer variable miles = 1000.0 # floating-point variable name = "Runoob" # string  

  • python allows you to assign values to multiple variables at the same time.
  # Creates an integer object with a value of 1, a value from the back forward, and three variables pointing to the same memory address. A = b = c = # Two Integer objects 1 and 2 are assigned to variables A and B, and the string object "Runoob" is assigned to variable C. A, b, C = 1, 2, "Runoob"  
  • Python3 have six standard data types:
    • number (number)
    • string (String)
    • list (list)
    • Tuple (tuple)
    • Set (collection)
    • Dictionary (dictionary)
  • Py Thon3 Six standard data types:
    • immutable Data (3): Number, String (string), tuple (tuple),
    • variable data (3): List (list), Dictionary ( Dictionary), set (collection).
Blank Line
  • A blank line is also part of the program code.
  • A blank line separates between functions or methods of a class, representing the beginning of a new piece of code.
  • The class and function entries are also separated by a line of blank lines to highlight the beginning of the function entry.
  • Blank lines are different from code indentation, and empty lines are not part of the Python syntax.
  • When you write without inserting a blank line, the Python interpreter runs without errors.
  • However, the purpose of a blank line is to separate the code of two different functions or meanings, which facilitates the maintenance or refactoring of future code.
Wait for user input
#!/usr/bin/python3 input("\n\n按下 enter 键后退出。")
  • In the above code, "\ n" will output two new blank lines before the result output.
  • Once the user presses the ENTER key, the program exits.
Show multiple statements on the same line
  • Python can use multiple statements in the same row, using semicolons between statements (;) split
#!/usr/bin/python3 import sys; x = ‘runoob‘; sys.stdout.write(x + ‘\n‘)
Print Output
  • Print default output is line-wrapping
  • If you want to implement no line break, add end= "" At the end of the variable
#!/usr/bin/python3 x="a"y="b"# 换行输出print( x )print( y ) print(‘---------‘)# 不换行输出print( x, end=" " )print( y, end=" " )print()
Import and From...import
  • Import the appropriate modules in Python using Import or From...import.
  • Import the entire module (somemodule) in the format: import somemodule
  • Import a function from a module in the format: from Somemodule import somefunction
  • Import multiple functions from a module in the format: from Somemodule import Firstfunc, Secondfunc, Thirdfunc
  • Import all functions in a module in the format: from somemodule import *

Python 03-python3 Basic Syntax

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.