Python是什么编程语言

一、python是什么编程语言。

编译型与解释型。

编译型:是把源程序的每一条语句都编译成机器语言,并保存成二进制文件,这样运行时计算机可以直接以机器语言来运行此程序,速度很快;

解释型:则是只在执行程序时,才一条一条的解释成机器语言给计算机来执行,所以运行速度是不如编译后的程序运行的快的. 这是因为计算机不能直接认识并执行我们写的语句,它只                   能认识机器语言(是二进制的形式)

编译型:一次性,全部编译成二进制文件。

解释型:当程序运行时,一行一行,字节码,二进制。

编译型:一次性,将全部的程序编译成二进制文件,然后在运行。
优点:运行速度快。
缺点:开发效率低,不能跨平台。

解释型:当你的程序运行时,一行一行的解释,并运行。
优点:调试代码很方便,开发效率高,并且可以跨平台。

缺点:运行速度慢。

python是一门动态解释性的强类型定义语言。

二、运行第一个Python程序。

 python 文件路径 如:Python D:\T1.py   

三、变量的定义、命名

变量:将一些运算中间结果存在内存,以便后续的程序使用。

1,数字,字母,下划线的任意组合。

2,不能以数字开头。

3,不能是Python中的关键字。     ['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']

4,可描述性。

5,建议下划线。

6,不要用中文,不要拼音。不能太长。

如:name = '晓梅'  

       age = 12  

       #下划线  age_of_oldboy = 56  

       number_of_students = 80    

四:常量 python中常量全部用大写表示     

     ID = '13060443542354'     BIR_OF_CHINA= 1949   

五、注释

单行注释:#

多行注释:"""被注释的内容"""或者是 '''被注释的内容'''  (要求成对出现)

六、基本数据类型。

数字 int    

age = 15

int1 = 22  

int2 = 10

int3 = int1 + int2  

int3 = int1 - int2

int3 = int1 * int2  

int3 = int1 / int2  

字符串 str  

name = '郭鹏达'   

name2 = "王岩"  

msg= '''  字符串  '''

人机交互input得到的数据 全是字符串。

数据带引号将值赋给变量,变量得到的值全是字符串。

字符串拼接 +  *

例一:
a = "jalf" b = a + "d" c = a + "dfa" print(a,b,c)
输出:
jalf jalfd jalfdfa

 

例二:
s1 = '111' s2 = '456' s3 = s1 + s2 + 'aaa' print(s1,s2,s3)
输出:
111 456 111456aaa

 

例三:
name = '坚强' s = name*8 print(s)
坚强坚强坚强坚强坚强坚强坚强坚强

 

 

布尔值: bool:True False

例四:
print
('1 > 2') print(1 > 2,type(1 > 2))
输出:
1 > 2 False <class 'bool'>

 

例五:
name = input("请输入你的名字:") hobby = input("请输入你的爱好:") s1 = "我的名字是" s2 = "我的爱好是" print(s1 + name,s2 + hobby)

 

七、格式化输出。 %占位符  s str字符串 d 数字(digit)

name = input("请输入你的名字:")
age = int(input("请输入你的年龄:"))
job = input("请输入你的工作:")
hobby = input("请输入你的爱好:")
msg = '''
-------info of %s-------
Name: %s
Age: %d
Job: %s
Hobby: %s
'''%(name,name,age,job,hobby)
print(msg)

 

#ps:如何将字符串转化成数字

int(str)条件:int(str)字符串转化成数字,str全部是数字组成的。

str(int)数字转化成字符串,str(1234) 无条件。

八、占位符。

%s str

%d digit

msg = '我叫%s,今年%d,我学习进度为10%%'%('数学哥',18)
print(msg)

 

九、if语句。

1、if 条件:  

          结果  

if 1 == 2:
    print('咱们去吃饭')
print(666)

2、if 条件:

        结果

    else:

        结果

name = int(input('请猜我手中的数字:'))
if name == 1:
    print('恭喜你猜对了')
else:
    print('你猜错了')

 

3、if  条件:

        结果

    elif  条件:

        结果

    elif  条件:

        结果

    elif  条件:

        结果

    else:

        结果

week = input('请输入数字:')
if week == '1':
    print('今天是星期一')
elif week == '2':
    print('今天是星期2')
elif week == '3':
    print('今天是星期3')
elif week == '4':
    print('今天是星期4')
elif week == '5':
    print('今天是星期5')
else:print('今天是周末')

 

score = int(input("输入分数:"))

if score > 100:
    print("我擦,最高分才100...")
elif score >= 90:
    print("A")
elif score >= 80:
    print("B")
elif score >= 60:
    print("C")
elif score >= 40:
    print("D")
else:
    print("太笨了...E")

循环中可以再套循环:

name = input('请输入你的名字:')
if name == 'xiaomei':
    if 1 == 1:
        print('哇,美女')
    else:print('一般般')
else:
    print('请输入正确的名字')

 

十、while循环。

while 条件:

     结果

flag = True
print('111')
while flag:
    print('')
    print('李白')
    print('丑八怪')
    flag = False
    print('666')
print(flag)
print(222)

 

如何跳出while循环:

1、改变条件,跳出循环。  

从1打印到100

count = 1
flag = True
while flag:
    print(count) count = count + 1 if count == 101: flag = False

或者

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

 如果在循环的过程中,因为某些原因,你不想继续循环了,怎么把它中止掉呢?

这就用到break 或 continue 语句
break用于完全结束一个循环,跳出循环体执行循环后面的语句
continue和break有点类似,区别在于continue只是终止本次循环,接着还执行后面的循环,break则完全终止循环        

2、break

  break 跳出循环,运行循环以外下面的程序。

print(111)
while True:
    print('')
    print('社会摇')
    print('江南')
    break
    print('你到底爱不爱我')
    #if  很多代码
print(222)

例,输出1到100

count = 1
while True:
    print(count)
    count += 1
    if count == 101:
        break

                

3、continue

跳出本次循环,继续下一次循环

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

 

count = 0
while count < 10:
    count += 1
    if count == 7:
        print ( )
        continue
    else:
        print(count)

 

第一天作业:

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

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

方法二:

count = 1
flag = True
while flag:
    if count == 7:
        print( )
        count += 1
    print(count)
    count = count + 1 
    if count == 11:
        flag = False

 

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

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

 

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

方法一:

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

方法二:

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

 

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

sum = 0
count = 1
while count < 100:
    if count % 2 == 1:
        sum = sum + count
    else:
        sum = sum - count
    count += 1
print(sum)

 

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

i = 3
flag = True
name = "alex"
pwd = "alex3714"
while flag:
    username = input("请输入你的用户名:")
    password = input("请输入你的密码:")
    if username == name and password == pwd:
        print("登录成功")
        flag = False
    else:
        print("用户名或密码错误,登录失败")
    i = i - 1
    if i == 0:
        flag = False

 

转载于:https://www.cnblogs.com/biluo/p/7705071.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值