首页 > 代码库 > python全栈之路【一】基础

python全栈之路【一】基础

第一句代码

Hello World

python3

print ("Hello World")

python2

print "Hello World"

linux

#!/usr/bin/env python
print ("Hello World")

变量

变量代指某一个变化的值

变量只能由字母,数字,下划线组成,并且不能由数字开头

python关键字不能做变量

name = Amanda
age = 21
print(name,age)

条件语句

if else

name = input("name:")
if name == Amada:
    print("Hello Amand")
else:
    pass

 while 循环

count = 0
while count < 100:
       print(count)
      count = count + 1

作业

1、使用while循环输入 1 2 3 4 5 6     8 9 10

count = 1

while count < 11:
   if  count == 7:
       pass
   else:
       print(count)
   count = count + 1

 

2、求1-100的所有数的和

s = 0
count = 0
while count <101:
    s = s + count
    count = count + 1
    print(count)
    print(s)

 

3、输出 1-100 内的所有奇数

count = 1
while count < 101:
     a = count % 2
     if a == 1:
         print (count)
     count = count +1

 

4、输出 1-100 内的所有偶数

count = 1
while count < 101:
     a = count % 2
     if a == 0:
         print (count)
     count = count +1

 

5、求1-2+3-4+5 ... 99的所有数的和

s = 0
c = 0
while c <100:
    q = c % 2
    if q == 0:
        s = s - c
    else:
        s = s + c
    c = c +1
print(s)

 

6、用户登陆(三次机会重试)

count = 0
while count < 3:
    name = input("账号:")
    passwd = input("密码:")

    if name == "ada" and passwd == "123":
         print("欢迎登陆")
         break
    else:
         print("账号或者密码错误")
    count = count + 1

 

python全栈之路【一】基础