Home >Backend Development >Python Tutorial >Introduction to Python ternary operations and Python functions

Introduction to Python ternary operations and Python functions

高洛峰
高洛峰Original
2017-03-10 18:59:101624browse

Ternary operation:

if 1+1 == 2 :
    print (True)
else:
    print (False) 
     
#等同于:
print (True if 1+1==2 else False)

Basic syntax of function def XX():

    定义函数
#  return aa  返回值
# 或 pass      什么也不返回
# XX()   调用函数
 
#函数的有三中不同的参数:
 
#------普通参数------
def func(name):  
    print (name)
 
func('fanhaibo')   
 
# ------默认参数------
 
def  func2(name,age=30):
    print (name,age)
 
func2('fanhaibo')
func2('张三',20)
 
# ------动态参数------
# *args
def func3(*args):
    print  (args)
 
func3(1)
func3('hello')
func3(1,2,3)
func3([1,2,3])
func3({1:'a',2:'b'})
#**kwargs:可以传入多个元素以key= value
def func4(**kwargs):
    print(kwargs)
 
func4(name='fanhaibo',age=30)  
 
#*args **kwargs
 
def func5(arg,*args,**kwargs):
    #print  (args)
    #print(kwargs)
    print  (arg,args,kwargs)
 
#把1传给arg,把’a‘’b‘’c‘作为元组传给args,把name、age传给kwargs
func5(1,'a','b','c',name='fanhaibo',age=30)


The above is the detailed content of Introduction to Python ternary operations and Python functions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn