码迷,mamicode.com
首页 > 编程语言 > 详细

python基础2018.12.3

时间:2018-12-03 21:38:28      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:基本   练习题   会员   函数   bre   程序   第一个字符   http   end   

print(hello word)
#!/usr/bin/env python

上述一行是linux系统下特有的,因此第一行必须有

#! -*-coding:utf8 -*-
print(李佳伦)

对于python2版本而言,需要打上面的语言,对于3版本则不需要。(因为2版本是默认用asc码)

二、变量定义的规则:

    • 变量名只能是 字母、数字或下划线的任意组合
    • 变量名的第一个字符不能是数字
    • 以下关键字不能声明为变量名
      [‘and‘, ‘as‘, ‘assert‘, ‘break‘, ‘class‘, ‘continue‘, ‘def‘, ‘del‘, ‘elif‘, ‘else‘, ‘except‘, ‘exec‘, ‘finally‘, ‘for‘, ‘from‘, ‘global‘, ‘if‘, ‘import‘, ‘in‘, ‘is‘, ‘lambda‘, ‘not‘, ‘or‘, ‘pass‘, ‘print‘, ‘raise‘, ‘return‘, ‘try‘, ‘while‘, ‘with‘, ‘yield‘]

三、条件语句

例子:

if 1==1:
    print("到付件")
else:
    print("发货")

1tab键=4个空格

例2:

if 1==1:
    print("大家好")
    print("大家减肥的")
print(end)

上述代表:执行if条件语句

                  执行if的代码块

                  继续执行下一语句

注意:if条件句可以嵌套!

例3:

inp = input(请输入会员级别)

if inp == "高级会员":
    print(1年)
elif inp == "白金会员":
    print(2年)
elif inp == "铂金会员":
    print(3年)
else:
    print(00)
    
print(开始)

 四、数据的基本类型:

1.字符串(涉及加法)

例1:启动cmd,进入python程序

n1="alex"
n2=sb
n3=n1+n2
print(n3)
alexsb

 (涉及乘法)

例2:启动cmd,进入python程序

n1="alex"
n2=n1*3

print(n2)
alexalexalex

例3:

a=13
temp=a%4
if temp==1:
    print(偶数)
else:
    print(奇数)

 !=      含义是:   “不等于”

2.数字

五、循环语句:

例1:

import time
while 1 == 1:
    print(time.time())

import 加上特定的python的函数

 

例2

import time
count=0
while count < 10:
    print(time.time())
    count=100
print(123)

例3:

import time
count=0
while count < 10:
    print(count)
    #count=100
    count=count+1
print(123)

 !!!练习题:

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

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

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

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

5.求1+2+....99的和

6.用户登录(三次机会尝试)

 

答案:用到了:if,while,奇偶数的知识

 

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

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

 

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

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

 

 

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

n=1
while n<101:
    temp=n%2
    if temp==0:
        pass
    else:
        print(n)
    n=n+1

 

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

n=1
while n<101:
    temp=n%2
    if temp==0:
        print(n)
    else:
        pass
    n=n+1

 

5.求1-2+3-4+5....99的和

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

 6.用户登录(三次机会尝试)

count=0
while count<3:
    user=input(>>>)
    pwd=input(>>>)
    if user==alex and pwd ==123:
        print(欢迎登录)
        break
    else:
        print(用户名或密码错误)
    count=count+1

 

补充:continue在while循环语句中的运用——继续进行下一个循环,不往下进行非循环任务
技术分享图片

 

count=0
while count<10:
    count=count+1
    print(count)
    continue
    print(11111111)

print(end)

补充:break在while循环语句中的运用

终止当前所有的循环,直接跳到下面进行

 

技术分享图片

 

python基础2018.12.3

标签:基本   练习题   会员   函数   bre   程序   第一个字符   http   end   

原文地址:https://www.cnblogs.com/lijialun/p/10059586.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!