2、循环判断

IF 判断

FOR循环

for i in range(1,5):
        print i
for i in range(5):
        print i

用变量控制循环次数:

  1. #!/usr/bin/python
    list=[‘lk‘,‘tom‘,‘jack‘]
    a=4
    for name in range(1,a):
            print name

小练习:for和if结合用

  1. [[email protected] python_scripts]# cat 4for.py
    #!/usr/bin/python
    list=[‘lk‘,‘tom‘,‘jack‘]
    for name in list:
            if name==‘lk‘:
                    print name,‘ is very good!‘
            else:
                    print name
    print ‘loop over!‘
    
    [[email protected] python_scripts]# python 4for.py
    lk  is very good!
    tom
    jack
    loop over!

WHILE循环

  1. [[email protected] python_scripts]# cat 5while.py
    #!/usr/bin/python
    whileTrue:
            input=raw_input(‘(alive|dead|other):‘)
            if input==‘alive‘:
                    print ‘The process is ‘,input
            else:
                    print ‘The process is ‘,input
                    break
时间: 2024-08-24 14:32:26

2、循环判断的相关文章

Python条件循环判断

1.条件判断语句 Python中条件选择语句的关键字为:if .elif .else这三个.其基本形式如下: 1 2 3 4 5 6 7 8 9 age_of_cc = 27 age = int(input("guessage:")) if age == age_of_cc:     print("Yes,you got it!") elif age > age_of_cc:     print("猜大啦!") else:     prin

shell文本过滤编程(六):awk之循环判断及数组

[版权声明:转载请保留出处:blog.csdn.net/gentleliu.Mail:shallnew at 163 dot com] 与上一节printf一样,awk的循环判断和C语言的循环判断语法极其类似. 1. While循环 #!/bin/sh awk ' BEGIN { ORS="" } { i=0 while (i < NF) { printf("* ") i++ } print "\n" } ' group_file1 首先,

VBA第六课 循环判断数据

Sub do_loop循环判断() Dim cj% cj = 1 Do cj = cj + 1 If cj > 10 Then Exit Do '退出循环 Else If Cells(cj, 2) >= 90 Then Cells(cj, 3) = "优秀" If Cells(cj, 2) >= 80 And Cells(cj, 2) <= 90 Then Cells(cj, 3) = "良好" If Cells(cj, 2) >= 6

禁止使用循环判断求1+2+3+..n

题目描述 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). 思路: 看到这一题的时候,不能使用乘除法和循环判断,当时就想到了使用二进制移位的方法,因为二进制不就只有加法操作,而且所有的计算在计算机里都是二进制操作. 首先:1+2+3+..+n  =   n(1+n)/2 其次需要了解一下二进制相关性质: 二进制数左移或右移N位等于扩大或缩小2的N次方倍 2进制的基数为2所以扩大10倍,相当于×2缩小10

shell循环判断语法

for循环 for循环数字1-100得出数字1到100的和 [[email protected] src]# cat fors.sh #!/bin/bash sum=0 for a in `seq 1 100` do ? sum=$[$a+$sum] done echo $sum [[email protected] src]# sh -x fors.sh + sum=0 ? ? ? ? ? ? 部分执行过程 ++ seq 1 100 + for a in '`seq 1 100`' + sum

if循环判断

if循环判断 if-else循环的语法格式 if 逻辑判断句: ? 代码块 # 缩进表示所属关系 else 逻辑判断句: ? 代码块 if 和elif同时使用来做多层判断 if 逻辑判断式: 代码块 elif 逻辑判断式: ? 代码块 if的嵌套使用 在if判断句中可以继续加入if的判断模块 例:if 逻辑判断1: ? 代码块 ? if 逻辑判断2: ? 代码块 ? else2: ? 代码块 ? else1: ? 代码块 循环控制的两种方式 while循环 语法 while + break wh

shell脚本进阶之循环判断

p.MsoNormal,li.MsoNormal,div.MsoNormal { margin: 0cm; margin-bottom: .0001pt; text-align: justify; font-size: 14.0pt; font-family: 等线 } h1 { margin-top: 15.6pt; margin-right: 0cm; margin-left: 0cm; margin-bottom: .0001pt; text-align: justify; line-he

python列表和循环判断

列表和循环 __author__ = 'Yimtsao'# coding: UTF-8list = [ ('iphone',8050), ('mac pro',12000), ('starbuck latte',31), ('alex python',82), ('bike',800),] #商品列表gouwuche = [] #空的购物车while True: wage = input("请输入你现有的金额:") if wage.isdigit(): #判断该字符串是否为数字 wag

Python的基本语法,涵盖数据类型、循环判断、列表、map和set等

以#开头的语句是注释 当语句以冒号“:”结尾时,缩进的语句视为代码块.一般缩进4个空格 Python程序是大小写敏感的,如果写错了大小写,程序会报错. Python的数据类型 整型 浮点型 字符串 布尔值 空值 变量 变量这块需要好好说明一下.Python属于动态语言,它的变量不需要指定类型,而java等属于静态语言,要想使用一个变量,必须为变量指明类型. 这样的话Python的变量就很简单了,像: a = 3 x = "你好" 等都是变量. Python的字符串 因为Python的诞