python03

C:Usersliuwe>python
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> if 1:
... print("1 is here!")
... print("not 2")
...
1 is here!
not 2
>>> a=0
>>> if a:
... print(True)
... else:
... print(False)
...
False
>>> q=2
>>> a=2
>>> if a==1:
... print(1)
... elif a==2:
... print(2)
... else:
... print("not 1 or2!")
...
2.

>>> range(10)
range(0, 10)
>>> for i in range(10)
File "<stdin>", line 1
for i in range(10)
^
SyntaxError: invalid syntax
>>> for i in range(10):
... print(i)
...
0
1
2
3
4
5
6
7
8
9
>>>

>>> for i in range(5,10):
... print(i)
...
5
6
7
8
9
>>> for i in range(1,10,2):
... print(i)
...
1
3
5
7
9
>>> for i in range(10,0,2):
... print(i)
...
>>> for i in range(10,0,-2):
... print(i)
...
10
8
6
4
2

>>> for i in range(5,10):
... print(i)
...
5
6
7
8
9
>>> for i in range(1,10,2):
... print(i)
...
1
3
5
7
9
>>> for i in range(10,0,2):
... print(i)
...
>>> for i in range(10,0,-2):
... print(i)
...
10
8
6
4
2
>>>

#记事本不用tab,用四个空格
#1.输入一个字符串,判断一下是大写,小写还是数字,还是其他字符
s=input("请输入一个字符: "):
if s>='A' and s<='Z':
  print("这是一个大写字母")
elif s>='a' and s<='z':
  print("这是一个小写字母")
elif s>='0' and s<'9':
  print("这是一个数字")
else:
  print("这是其他字符")


2.判断一个字符,4个分支,通过使用循环方式,需要把四个分支依次做一个遍历
for i in range(4):
  s=input("请输入一个字符: "):
  if s>='A' and s<='Z':
    print("这是一个大写字母")
    continue #结束本次循环,继续下次循环
  elif s>='a' and s<='z':
    print("这是一个小写字母")
  elif s>='0' and s<'9':
    print("这是一个数字")
    break #结束所有的循环
  else:
    print("这是其他字符")
print("find something“)

while学习

i=0
while i<4:
  print(i)
  i+=1


i=10
while i>0:
  print(i)
  i-=1


练习:while写一个死循环,每次循环输入一个字符,
如果是字母,就保留再一个字符串中,不断累加,a,b,c=>abc
如果是数字,continue
如果是* ,break结束循环
思考:
while表达式:
代码块
1:死循环怎么写
while 1:
代码块
当某一个条件出发的时候=》break
while 1:
s=input("input your command:")
print(s)
if s=="bye!":
  print("bye!!!")
  break

2.result=" "
while 1:
  s=input("input your char:")
  print(s)
if s=="*":
  print("bye!!!")
  break
else:
  if (s>="a" and s<="z") or (s>="A" and s<="Z"):
    result+=s #result=result+s
  elif s>="0" and s <="9":
    continue
print("final result:",result)

for的用法:执行指定次数的循环,for 优先,其次while
while用法:不知道执行具体的次数,while死循环模式+某个条件的break

原文地址:https://www.cnblogs.com/JacquelineQA/p/14071324.html