首页 > 编程语言 >Review-python-note2

Review-python-note2

时间:2022-10-10 18:33:41浏览次数:61  
标签:python Review note2 else while boolean statement expression

Note 2 - body and structure

标签(空格分隔): python


Learning techniques

Assuming you are playing << call of duty >> and need go through one difficult scene where has many options and the duplicate operations like pulling trigger.

1.Condition statement

1.1 Definition

#syntax
#boolean expression :a cimbination of comparsion operators and logic operators
if boolean expression :
elif boolean expression :
else :

Example:

# if 
age = 20
if age < 18 :
    print("youngster")
elif age == 18 :
    print("18")
else:
    print("adult")

1.2 deeper intuition

If statement exactly follows the sementic rules as if these statements looks like a short story!

if bool(true)->keep going
bool(false)->else / elif(iteration)

Extra point: input()

a = input("please input your age!")

The terminal prompts you to input age-> that means you need to type your age on the console.

1.3 nested if statement

Inner one if statement is nested into the outer if statement

if boolean expression :
    if boolean expression :
    #....

2. loop statement

  1. while
  2. for

keywords:

  1. continue
  2. break
  3. else
  4. pass

2.1 while

Example:

i = 1
while i <= 10 :
    print("still in loop body",i)
    i += 1
    if i == 10 :
        break

else :
    print("jump out of loop then execute else")

We can single out some important points:

1.syntax

while boolean-expression :
    loop body
    continue # stop this sub-course and jump directly to next sub-course
    break # terminate the whole loop and execute the next statement
else:
##else executed if the entire lopp is executed but jump over to the underneath statement if loop ##body executes break

2.2 for

Example:

for element in collection :
    continue # same as while 
    break # same as while
else:
    # same as while when break within loop body is executed makes else block cannot be executed 

When I first learned to program with python in the way as I do in Java,especially retrieving the entire list in python.I hardly got used to code for statement in python.But a suggestion is given for this problem.

for i in range(10)[
# equivalent to ->
# for (int i = 0 ; i < a.length ; i++ )

[1]

2.3 pass

As result of facilitating the complement for code in the future,we can use keyword pass to occupy some space to be available later.

3.iterator and generator

what we learn here is just getting our feet wet.

3.1 Iterator

Iterator is for retrieving the whole element in a collection like list and so forth.
There are two crucial functions of it: iter() [exclusive]and next()[general]

a = [1,2,3,4,5,6,7,8,9,10]

iterator = iter(a)

while True :
    print(next(iterator),end=",")

The terminal shows:

1,2,3,4,5,6,7,8,9,10,Traceback (most recent call last):
  File "d:\workspace\python\learning_06.py", line 6, in <module>
    print(next(iterator),end=",")
StopIteration

3.2 generator

pass block


  1. range([start],end,[step]) generates a number list :start from [start],end up to end,go forward one timee with one step ↩︎

标签:python,Review,note2,else,while,boolean,statement,expression
From: https://www.cnblogs.com/UQ-44636346/p/16776771.html

相关文章

  • Python3 函数
    函数特点:完成某一个特定的功能代码重用保持一致性,易维护,可扩展性一函数定义数学函数python函数定义f(x)=2x+1deff(x):               #函数定义,f......
  • python(函数参数与名称空间)
    今日内容概要函数参数名称空间与作用域位置参数,关键字参数,默认参数,可变长参数,命名关键字参数名字的查找顺序今日内容详细函数参数之位置参数​ 在调用函......
  • python接口自动化-pytest常用命令
    1.pytest拥有丰富的入参选项,常用参数如下:-m:只运行被标记的测试用例;-k:只运行与给定字符串表达式匹配的类名下的测试用例;-s:显示标准输出,例如print()的语句;-v:显示详细报告......
  • python函数2
    今日内容概要函数参数名称空间与作用域名称的查找顺序函数名的多种用法今日内容详细函数参数值位置参数"""补充:当子代码只有一行并且很简单的情况下可以直......
  • Python函数-4
    一.不定长参数难度:3星Python函数中有两种不定长参数,第一种是*X,返回值是元祖类型,在传入额外的参数时可以不用指明参数名,直接传入参数值即可。第二种是**X,这种类型返回的是......
  • Python函数-4
    一.不定长参数难度:3星Python函数中有两种不定长参数,第一种是*X,返回值是元祖类型,在传入额外的参数时可以不用指明参数名,直接传入参数值即可。第二种是**X,这种类型返回的是......
  • Python函数-4
    一.不定长参数难度:3星Python函数中有两种不定长参数,第一种是*X,返回值是元祖类型,在传入额外的参数时可以不用指明参数名,直接传入参数值即可。第二种是**X,这种类型返回的是......
  • Python-pymysql操作MySQL数据库
    一、安装pymysqlpy-mpipinstallpymysql;二、pymysql数据库操作1.简单示例#coding=utf-8importpymysql#打开数据库连接conn=pymysql.connect(host=......
  • Python基础11
    今日内容概要函数参数名称空间与作用域名字的查找顺序今日内容详细函数参数位置参数及关键字参数位置形参 函数定义阶段括号内从左往右依次填写的变量名 deffu......
  • python连接tdengine数据库
    1、首先在pyCharm中install(我的做法)或者: pipinstallC:\TDengine\connector\python\windows\python3(参考 https://blog.csdn.net/Tomonkey/article/details/10880791......