Python
Pythonprint()
- def ()
- return [] return None
def functionname( parameters ):
"_"
function_suite
return [expression]
Python
(Python 2.0+)
def printme( str ):
""
print str
return
Python
printme
(Python 2.0+)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
#
def printme( str ):
""
print str
return
#
printme("!")
printme("")
!
python
a=[1,2,3]
a="Runoob"
[1,2,3] List "Runoob" String a List String
(mutable)(immutable)
python strings, tuples, numbers list,dict
a=5 a=10 int 10 a 5 aa
la=[1,2,3,4] la[2]=5 list la la
python
c++ funaaa funa a a
c++ funla la funla
python
python
(Python 2.0+)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
def ChangeInt( a ):
a = 10
b = 2
ChangeInt(b)
print b # 2
int 2 b ChangeInt ba b Int a=10 int 10 a
(Python 2.0+)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
#
def changeme( mylist ):
""
mylist.append([1,2,3,4])
print ": ", mylist
return
# changeme
mylist = [10,20,30]
changeme( mylist )
print ": ", mylist
: [10, 20, 30, [1, 2, 3, 4]] : [10, 20, 30, [1, 2, 3, 4]]
printme()
(Python 2.0+)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
#
def printme( str ):
""
print str
return
#printme
printme()
Traceback (most recent call last):
File "test.py", line 11, in <module>
printme()
TypeError: printme() takes exactly 1 argument (0 given)
Python
printme()
(Python 2.0+)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
#
def printme( str ):
""
print str
return
#printme
printme( str = "My string")
My string
(Python 2.0+)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
#
def printinfo( name, age ):
""
print "Name: ", name
print "Age ", age
return
#printinfo
printinfo( age=50, name="miki" )
Name: miki Age 50
ageage
(Python 2.0+)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
#
def printinfo( name, age = 35 ):
""
print "Name: ", name
print "Age ", age
return
#printinfo
printinfo( age=50, name="miki" )
printinfo( name="miki" )
Name: miki Age 50 Name: miki Age 35
2
def functionname([formal_args,] *var_args_tuple ):
"_"
function_suite
return [expression]
*
(Python 2.0+)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
#
def printinfo( arg1, *vartuple ):
""
print ": "
print arg1
for var in vartuple:
print var
return
# printinfo
printinfo( 10 )
printinfo( 70, 60, 50 )
: 10 : 70 60 50
python lambda
- lambdadef
- lambdalambda
- lambda
- lambdaCC++
lambda
lambda [arg1 [,arg2,.....argn]]:expression
(Python 2.0+)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
#
sum = lambda arg1, arg2: arg1 + arg2
# sum
print " : ", sum( 10, 20 )
print " : ", sum( 20, 20 )
: 30 : 40
return
return[]returnNone
(Python 2.0+)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
#
def sum( arg1, arg2 ):
# 2."
total = arg1 + arg2
print " : ", total
return total
# sum
total = sum( 10, 20 )
: 30
(Python 2.0+)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
total = 0 #
#
def sum( arg1, arg2 ):
#2."
total = arg1 + arg2 # total.
print " : ", total
return total
#sum
sum( 10, 20 )
print " : ", total
: 30 : 0
bling coin
zha***[email protected]
#!/usr/bin/python # -*- coding: UTF-8 -*- globvar = 0 def set_globvar_to_one(): global globvar # global globvar = 1 def print_globvar(): print(globvar) # global set_globvar_to_one() print globvar # 1 print_globvar() # 1 globvar1global---
2global global x, y, z
bling coin
zha***[email protected]
BMPixel
194***[email protected]
:
Python2
#!/user/bin/python # -*- coding: UTF-8 -*- def reverse(li): for i in range(0, len(li)/2): temp = li[i] li[i] = li[-i-1] li[-i-1] = temp l = [1, 2, 3, 4, 5] reverse(l) print(l)Python3
#!/user/bin/python3 def reverse(li): for i in range(0, len(li)//2): temp = li[i] li[i] = li[-i-1] li[-i-1] = temp l = [1, 2, 3, 4, 5] reverse(l) print(l)BMPixel
194***[email protected]
King
kin***63.com
:
def reverse(ListInput): RevList=[] for i in range (len(ListInput)): RevList.append(ListInput.pop()) return RevListKing
kin***63.com
songy
son***[email protected]
def reverse(li): for i in range(0, len(li)/2): # python3 len(li)//2 li[i], li[-i - 1] = li[-i - 1], li[i] l = [1, 2, 3, 4, 5] reverse(l) print(l)songy
son***[email protected]
shiyeweimian
860***[email protected]
return fun return fun()
>>> def funx(x): def funy(y): return x * y return funy #return funyfunxfuny >>> funx(7)(8) 56 >>> def funx(x): def funy(y): return x * y return funy() #return funy()funy >>> funx(7)(8) Traceback (most recent call last): File "<pyshell#5>", line 1, in <module> funx(7)(8) File "<pyshell#4>", line 4, in funx return funy() TypeError: funy() takes exactly 1 argument (0 given) >>> def funx(x): def funy(y): return x * y return funy(8) >>> funx(7) 56shiyeweimian
860***[email protected]
santiago
408***[email protected]
python
[1,2,3] List "Runoob" String a List String
santiago
408***[email protected]
Sonnet
gra***[email protected]
DocStrings
#!/usr/bin/python # -*- coding: UTF-8 -*- def function(): ''' say something here ''' pass print (function.__doc__) # docDocStrings
Python (DocStrings)
Sonnet
gra***[email protected]
python
495***[email protected]
#!/usr/bin/python # -*- coding: UTF-8 -*- def printme( str, int ): print str,int return # int, str print printme(int=10, str = "My string")My string 10
Python
python
495***[email protected]
axi***[email protected]
#!/usr/bin/python3 def arras(x,y,z=4,*param,**params): print(x,y,z) for i in range(len(param)): print(param[i]) for j in params: print(j+':'+params[j]) arras(1,2,3,4,5,'Python (Google)Google ,.',foo='foo1=100',h00='hoo1=200',koo='koo1=4000')* **
axi***[email protected]
sunird
118***[email protected]
add add_twice
sunird
118***[email protected]
yefeidd
zjs***[email protected]
yefeidd
zjs***[email protected]
hamilton
123***c.com
/
Python heap list
Python
hamilton
123***c.com