Python入门练习笔记【2022-3-6】

简单记录Python与Java的不同点

  1. Python 是解释型、动态数据类型语言,而Java是 编译->字节码文件->JVM解释执行,并且数据类型更加严谨。
  2. Python 的List集合为[].
  3. Python的tuple类型为(),类似Java的数组.
  4. Python 使用格式对齐与:来区分代码块,而Java等语言使用大括号{}
  5. 选择结构、循环结构的语句也不太一样。
  6. Python 函数的定义使用def,返回值可以任意类型,任意个数。另外使用关键字pass代表空代码块,如果什么也不写,会运行报错。
  7. Python 异常处理关键字
    try:
        #code
    except Exception as e:
        #handle
    finally:
         #handle
    
    另外还有手动抛出异常关键字raise
  8. Python 文件声明编码:# -*- coding:utf-8 -*-
  9. Python 文件在Linux 指定Python环境 直接执行:#!/usr/bin/env python3,不过记得要给执行权限:chmod a+x [filename]
  10. Python 中的布尔类型首字母都是大写的:True False,另外代表空值的关键字None也是大写。
  11. Python 大小写敏感,这点与Java一致。
  12. Python 中没有i++++i,自增自减都没有,但是可以i+=1
  13. Python中只有模块、类、函数才会新增作用域,另外注意globalnonlocal的区别

实际练习

#! /usr/bin/env python3
# -*- coding:utf-8 -*-
#become byte data by encoding of 'utf-8', then to transfer in network.
x="Hello World".encode("utf-8")
print(x) #Result is "b'Hello World'", which became a byte data.
x.decode('utf-8') #become a string date by decoding of 'utf-8' from byte data.
print(x)
sign=input('''Please input
the sign:''')
if sign=='1' :
    print("I love %s" % "you")
else :
    print("I hate %s %4.2f" % ("you",1314))
print('----------------')
score=input("Please input XiaoMing's score:")
score=float(score)
#String Format Method
print("Score:%10.1f%%\n" % score)
print("Score:{0:15.1f}%\n".format(score))
print(f"Score:{score:20.1f}%\n")
print('-----------')

print('---List---')
classmates=['Jason',123,['XiaoMing','XiaoHuan']]
classmates.append('aaa')
classmates.pop() #delete the end element----'aaa'
print(f'''classmates:{classmates}
{classmates[-1][-1]}''')
print(f"length:{len(classmates)}")

print('---tuple---') # Tuple like array object of java is static and final.
tuple_1=()
tuple_2=(1,)
tuple_3=(1,'2',['123',123])
print(f"tuple_3:{tuple_3}")
print("{0}".format(tuple_3[0]))
print("length:",len(tuple_3))


#小明身高1.75,体重80.5kg。请根据BMI公式(体重除以身高的平方)帮小明计算他的BMI指数,并根据BMI指数:
height=1.75
weight=80.5
bmi=weight/(height**2)
result=''
if bmi>0 and bmi<18.5 :
    result='lower';
elif bmi<25 :
    result='normal'
elif bmi<28:
    result="overweight"
elif bmi<32 :
    result="fat";
else :
    result="severe obisity"
print(bmi,result)


print('----------')
sum=0
ls=list(range(101)) # The function range is to generate a series number order, which starts from 0 with 1 per step.
for x in ls :
    sum+=x
print(f"SUM={sum}")


ls_2=list(range(10))
i=len(ls_2)-1
while i>=0 :
    if ls_2[i]==3 :
        i-=1
        continue
    elif ls_2[i]==1 :
        break
    else :
        print(f"{ls_2[i]}")
        i-=1


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员杰森

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值