Python learning- Conditional Execution & Functions

Python小白的学习之路 Day2

  • Conditional Execution (Chapter 3)
  • Functions (Chapter 4)

Conditional Execution (Chapter 3)

Comparison Operators

PythonMeaning
" <= "Less than or Equal to
" == "Equal to
" >= "Greater than or Equal to
" > "Greater than
" != "Not equal
" < "Less than

Indentation

  • Increase indent indent after an if statement or for statement (after : )
  • Maintain indent to indicate the scope of the block (which lines are affected by the if/for)
  • Reduce indent back to the level of the if statement or for statement to indicate the end of the block

Nested Decisions

if x > 1 :
    print('More than one')
    if x < 100 : 
        print('Less than 100') 
print('All done')

Two-way Decisions with else

if x > 2 :
    print('Bigger')
else :
    print('Smaller')

print('All done')

Multi-way with elif

if x < 2 :
    print('small')
elif x < 10 :
    print('Medium')
else :
    print('LARGE')
print('All done')
  • No else is also acceptable
if x < 2 :
    print('Small')
elif x < 10 :
    print('Medium')

print('All done')

The try / except Structure

  • If the code in the try works - the except is skipped
  • If the code in the try fails - it jumps to the except section
  • Sample try / except :
rawstr = input('Enter a number:')
try: 
    ival = int(rawstr)
except: 
    ival = -1

if ival > 0 :  
    print('Nice work')
else:  
    print('Not a number')

Functions (Chapter 4)

Python Functions

  • Built-in functions that are provided as part of Python - print(), input(), type(), float(), int(), max() …
  • Functions that we define ourselves and then use

Building our Own Functions

  • Once we have defined a function, we can call (or invoke) it as many times as we like
  • This is the store and reuse pattern

Arguments

  • An argument is a value we pass into the function as its input when we call the function

Parameters

  • A parameter is a variable which we use in the function definition. It is a “handle” that allows the code in the function to access the arguments for a particular function invocation.

注意 ArgumentsParameters 的区别

Return Values

  • Often a function will take its arguments, do some computation, and return a value to be used as the value of the function call in the calling expression. The return keyword is used for this.
  • The return statement ends the function execution and “sends back” the result of the function

Multiple Parameters / Arguments

  • We match the number and order of arguments and parameters
    Example:
def addtwo(a, b):
    added = a + b
    return added

x = addtwo(3, 5)
print(x)
8

Void (non-fruitful) Functions

  • When a function does not return a value, we call it a “void” function
  • Functions that return values are “fruitful” functions
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

J.Sampson

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值