python - Program Structure

First, the program structure

• Define the Head python template file:
add the header template code File-> Settings-> Editor-> File and Code Templates-> Python script script.

 

• Python using the same "indentation" to distinguish the code blocks, each layer indented four spaces to the right.

# With other common programming languages, Python {} is not used to represent code segments, but with the same indentation to distinguish
# indented to the right of each layer 4 spaces. Indenting a start block, the non-indented represents the end of a block.
sex = "male"
Subject = Subject # 1: 1, 2 languages, mathematics
if sex == "female":
Print (. "She is a teacher")
IF == 1 Subject:
Print ( "She is a language The teacher ")
elif Subject == 2:
Print (" she was a math teacher ").
the else:
Print (." he is a teacher ")
IF == 1 Subject:
Print (" he is a language teacher . ")
elif Subject == 2:
Print (" he is a math teacher ").
Print ()


• Use the "blank line" to separate logically related code.

# Can use an "empty row" code associated logical partition
# variable names all lowercase, connected to respective word by underlining
class_id = 1 # Class ID
student_sex = "F"
IF student_sex == "F":
IF class_id ==. 1:
Print ( "she was seven (1) students in the class")
the else:
Print ( "she was seven (2) classes of students")
the else:
IF class_id == 1:
Print ( "he was seven (1) students in the class")
the else:
print ( "he was seven (2) classes of students")
Print ()

 

Common newline problem

# String newline '\ the n-'
Print ( "Students in this class are very united. \ N get a total score of the first results in the school held the Games in co-! \ N students, teachers, and the school parents are very happy, take pictures together! \ n ")

Newline codes # '\', also known as line continuation character, the code for connecting the current line and the next line.
# NOTE: '\' can not add comments later.
print ( "Students in this class are very united. \
obtain a total score of the first results in the school held the Games in co-! \
students, teachers, and parents to the school are very happy to take pictures together!")

# Parentheses (), brackets [], braces {} statement contains up without using a backslash \
month_names = [ 'January', 'February', 'March',
'April', 'On May', 'June',
'July', 'August', 'September',
'October', 'November', 'December']

# Line multiple statements (not recommended, readability difference)
A =. 3; B = 2; C = A + B; Print ( "= C", C, "\ n-")

# Python divided into single-line comments in the comments and multiline comments
# This is a single-line comment, the following defines two constants
UNIT_PRICE_OF_APPLE = 7.9 # Apple's unit price constant name in all uppercase letters, each connected by underlining the word
UNIT_PRICE_OF_ORANGE = 6.8 # oranges unit price


• Python divided into single-line comments in the comments and multiline comments.

Single line comment using the "#" multi-line comments could be three single quotes may use three double quotes.

'' '
Which is a multi-line comment, three may be used in single quotes, three may also be used in double quotes
input () is a built-in input function python. It reads from the standard input line of text, the default standard input is a keyboard.
Argument is waiting message when user input is optional.
All data entered by the user are returned in the form of a string.
Here, you need to enter a value, it must be cast to enter the result
'' '
NUM = the INPUT ( "Please enter Apple's sales:")
AMOUNT UNIT_PRICE_OF_APPLE * = float (NUM)
Print ( "Apple's total sales:" , amount, "element", end = '\ n \ n')

• Python built-in function input () and print (): Input / output data

"" "
Print () function is used to output data formatting.
Values: represents a plurality of output
sep: indicates the separator between the plurality of output information, the default is a blank
end: represents all information is added after the output terminator, the default is a newline
"" "
NUM = the iNPUT (" Please enter sales oranges: ")
AMOUNT UNIT_PRICE_OF_ORANGE * = float (NUM)
Print (" total sales of oranges is: ", amount," yuan " = On Sep "")
INPUT ()


#% Placeholders use forms, format string
print ( "total sales of orange:% f Element"% amount)
INPUT ()
print ( "total sales of orange:.% 2f element"% amount )
INPUT ()
Print ( "sales of orange:% s kg, total sales of:.% 2f element"% (num, amount)) # operator% is a relatively old format method
INPUT ()
Print ( "total sales oranges is:.% 2f yuan", amount)

INPUT ()
# python3 in the most preferred method is to string formatting str.format ()
# {} and the characters inside (referred formatted field) will be the format parameter () replaces
# 1, by default order parameter corresponding to the output
print ( "major search engines are: {} and {} \ n" .format ( 'Baidu', 'the Google'))
# 2, a position designated by the format of parameters in parentheses
print ( "major search engines are: {0} and {1}". format ( 'Baidu', 'the Google'))
print ( "major search engines are: {1} and {0} \ n" .format ( 'Baidu', 'the Google'))
#. 3, to the parameter definition keyword, and other means used in combination with
print ( "search engines: {0}, {1} and {sou_gou}, etc." .format ( ' Baidu ',' Google ', sou_gou = ' search dogs'))

Guess you like

Origin www.cnblogs.com/Teachertao/p/11204365.html
Recommended