#后面写注释
Plus. 0 is automatically treated as float
Output int:
Print "He ' s%d pounds heavy."% My_weight
Output string:
Print "He ' s got%s eyes and%s hair."% (My_eyes, My_hair)
with the%r for debugging, since it displays the ' raw ' data of the variable, but the others is used for displaying to users.
Both single and double quotation marks can be used to represent strings.
%s can output Chinese (before the first line is added with #coding:utf-8 in the output Chinese)
Output Multiple lines:
Print "" "There ' s something going on here. With the three double-quotes. We are able to type as much as we do. Even 4 lines if we want, or 5, or 6. ""
Input:
Age = Raw_input ()
Type conversions:
x = Int (raw_input ())
the input () function would try to convert things you enter as if they were Python code, but it Have security problems so should avoid it.
Output to the screen before entering:
Age = Raw_input ("What old is You?")
Import
From sys import Argvscript, first, second, third = argv #将参数赋给这些变量
Common file manipulation functions:
Close--Closes the file. Like File->save. In your editor.
Read-Reads the contents of the file. You can assign the result to a variable.
ReadLine--Reads just one line of a text file.
Truncate--empties the file. Watch out if you are about the file.
Write (' stuff ')--writes "stuff" to the file.
# this one is like your scripts With argvdef print_two (*args): arg1, arg2 = args print "Arg1: %r, arg2: %r" % (ARG1, ARG2) # ok, that *args is actually pointless, we can just do thisdef print_ Two_again (ARG1, ARG2): print "arg1: %r, arg2: %r" % (ARG1, ARG2) # this just takes one argumentdef print_one (arg1): print "Arg1: %r" % arg1# this one takes no Argumentsdef print_none (): print "I got nothin '."
str () can be converted to string
if cars > people: print "We should take the cars." elif cars < people: print "We should not take the cars. " else: print "We can ' T decide." the_count = [1, 2, 3, 4, 5]fruits = [' apples ', ' oranges ', ' Pears ', ' apricots ']change = [1, ' pennies ', 2, "dimes ', 3, ' quarters ']# this first kind of for-loop goes through a listfor number in the_count: print "this is count %d" % numberwhile i < 6: print "At the top i is %d " % i numbers.append (i)
Every if-statement must has an else.
If This else should never run because it doesn ' t do sense and then you must use a die function in
the else that prints out a error message and dies, just like we do in the last exercise. this would find many errors.
Never nest if-statements more than, and always try to do them one deep.
Treat if-statements like paragraphs, where each if-elif-else grouping are like a set of
sentences. Put blank lines before and after.
Your Boolean tests should is simple. If They is complex, move their calculations to variables earlier on your function and use a good name for the Variab Le.
Use a while-loop only to loop forever, and that means probably never. This is only applies to Python; Other languages is different.
Use a for-loop for all other kinds of looping, especially if there are a fixed or limited number of things to loop Over.
Python Basic Grammar Notes