Python basic syntax

Source: Internet
Author: User

Grammatical features:-------------------------------------------------------------
Strict indent definition variable direct write variable name
No + + +--&& | | Instead of and or not
After judging the conditions need to add:
Global variables outside the function are required to be added in the function

4 data types-integers, long integers, floating-point numbers, and complex numbers:---------------------------------------------------------------------
2 is an example of an integer.
Long integers are just larger integers.
3.23 and 52.3E-4 are examples of floating-point numbers. The e tag represents a power of 10. Here, 52.3E-4 means 52.3 * 10-4.
( -5+4j) and (2.3-4.6j) are examples of complex numbers.
String: Single quotation mark "double quote" "triple quote" "-----------------------------------------------------------------------------
Single quotation marks are like double quotes, and three quotes can be written in multiple lines
Strings connected to strings: two strings are added automatically
A string is connected to a variable, and a space is automatically added between the two.

Escape character: \ is also a newline character R or R ignore escape
------------------------------------------------------------------------------

Control statement:------------------------------------------
For
----------------------------------------------------------------
Number = 23
guess = Int (raw_input (' Enter an Integer: '))
If guess = = number:
print ' Congratulations, you guessed it. ' # New block starts here
Print "(but don't win any prizes!)" # New block ends here
Elif Guess < number:
print ' No, it's a little higher than that ' # another block
Else
print ' No, it's a little lower than that '
print ' Done '

While
----------------------------------------------------------------------------------------------
Number = 23
running = True
While running:
guess = Int (raw_input (' Enter an Integer: '))
If guess = = number:
print ' Congratulations, you guessed it. '
running = False # This causes the and loop to stop
Elif Guess < number:
print ' No, it's a little higher than that '
Else
print ' No, it's a little lower than that '
Else
print ' The while loop was over. '
For
----------------------------------------------------------------------------------------------
For I in range (1, 5):
Print I
Else
print ' The For loop was over '
Break
-----------------------------------
While True:
s = raw_input (' Enter something: ')
if s = = ' quit ':
Break
print ' Length of the string is ', Len (s)
print ' Done '
Continue
-------------------------------------------
While True:
s = raw_input (' Enter something: ')
if s = = ' quit ':
Break
If Len (s) < 3:
Continue
print ' Input is of sufficient length '

Function:
--------------------
Use external variables plus global in functions
The default value is connected directly after the parameter
Default returns None
-----------------------------------
def say (message= "Hello World"):
print ' say ', message
SayHello ("Hello")
--------------------------
def func (A, b=5, c=10):
print ' A is ', A, ' and B are ', B, ' and C is ', C
Func (3, 7)
Func (c=24)
Func (c=50, a=100)------------------------------
def maximum (x, y):
If x > Y:
return x
Else
return Yprint Maximum (2, 3)
Module:----------------Each Python script is a module, we can introduce other modules into our own script so that we can use the functionality of the Introduction module directly. In contrast to Java, like every Python script is a Java class, we import other classes inside our own classes, Defining a module yourself is equivalent to writing a class and then introducing it into another class file---------------------------------------------------------------------------------------------- The--------------------------#!/usr/bin/python#coding=utf-8 import sys #使用模块 SYS module contains functions related to the Python interpreter and its environment, print SYS.A RGV-----------------------------------dir () function: —————-----list all the properties and functions within the module



Python 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.