Python了解及数值类型

1.首先进行ipython和pycharm的安装

ipython

 从网上下载软件

[ipython](https://pan.baidu.com/s/1nuGVZol)

•-shell终端中执行命令`ipython`试试效果;

pycharm

• 访问pycharm官网:http://www.jetbrains.com/pycharm/

•下载pychrm社区版本,安装使用,后面我们专门写一个章节,来描述pycharm常用的操作、设置以及快捷键。

这样设置模板:


效果如下:


2.python的输出与输入

1. python的输出:

# python2: print "要打印的字符串"

# python3: print("要打印的字符串")

name = "python"
print("hello %s" %(name))
sid = 7
print("hello %.3d" %(sid))
2. python的输入:
# python2:
# raw_input(): 接收字符串的数据;

# input(): 只能接收数值;类型;

# python2的操作 

name = raw_input("input name:")

print name

info = input("input:")

print info

# python3操作: 没有raw_input,只有input,
# python3中为了避免记忆困难, 用input接收数据, 为字符串类型;
name = input("Name:")
print(name)
age = input("Age:")

print(age,type(age))


3. python中支持的数值类型: int,long,float,complex
aInt = 2018

查看变量的数据类型

print(type(aInt))
aLong = 11223344556677889900998877665544332211
print(type(aLong))
# 在python3中不存在1L
#  强制设置数值为长整形, 后加L/l;
# bLong = 1L
# cLong = 1l

# print(type(bLong))

aFloat = 1.6688
print(type(aFloat))
# '2018+18e2'
bFloat = 18e+2
cFloat = 18e-2
dFloat = 18e2
# eFloat = 18e   # 不是浮点数, ae+b(a,b为常量, 代表a*10^b)
print(bFloat)
print(cFloat)
print(dFloat)
# 复数类型: x**2>0, x**2=-1    # a+bi
aComplex = 4+5j
print(type(aComplex))
print(aComplex.real)
print(aComplex.imag)

print(aComplex.conjugate())

# 2. 数据类型的转换: 在python中, 所有的数据类型,都可以作为内置函数, 转换数据类型;
aInt = 8
transFloat = float(aInt)
print(transFloat)
print(int(8.0))
print(complex(8))

# 3. 如何删除内存中的变量;
print(aInt)

print("deleting aInt.......")


4.Python运算符

符:+,-,*,**, /, %, //

赋值运算符:=, +=, -=, /=, *=, %=

关系运算符: >, >=, <, <=,!=, ==

与and, 逻辑或or, 逻辑非not


5.分支语句及其扩展

#if 条件表达式:
#  满足表达式执行的内容
#if 条件表达式:
#  满足表达式执行的内容
#else:

#  不满足表达式执行的内容

# 三目
# 有多个条件表达式
if  xxxx:
    pass
elif xxxx:
    pass
elif xxxx:
    pass
else:
    pass
a = 1
b = 2
# 三目运算符: print a>b?a:b
a = 3
b = 2
# 三目运算符的变种
print(a if a>b else b)
if a>b:
    print(a)
else:

    print(b)



6.if-elif应用

# 求平均成绩
#   1. 用户输入某个学生的姓名;
#   2. 输入该学生的语文, 数学与英语的成绩;
#   3. 打印:   姓名的成绩等级XXXX:
# avg_score
# 90~100   A
# 80~90    B
# 70~80    C
# 0~70     D
# other :   invaild score

stuName = input("姓名:")
chinese = float(input("语文成绩:"))
math = float(input("数学成绩:"))
english = float(input("英语成绩:"))
avgScore = (chinese + math + english) / 3
if avgScore>90 and avgScore<=100:
if 90 < avgScore <= 100:
    print("%s的等级为%s" % (stuName, 'A'))
elif 80<avgScore<=90:
    print("%s的等级为%s" % (stuName, 'B'))
elif 70<avgScore <=80:
    print("%s的等级为%s" % (stuName, 'C'))
elif 0<avgScore <=70:
    print("%s的等级为%s" % (stuName, 'D'))
else:

    print("%s的成绩是无效的" %(stuName))


7.Python循环语句

# 1.while 条件表达式:
#     pass
count = 0
while count < 4:
     print("hello world")
     count += 1  # count = count + 1

# 2. 循环语句的第二个问题: 如何跳出循环?
# break:
# continue:
#import getpass
tryCount = 0
while tryCount < 3:
    username = input('用户名:')
    passwd = input('密码:')
    #passwd = getpass.getpass("密码:")
    if username == 'root' and passwd == 'redhat':
        print('login ok')
        break

    tryCount += 1


continue应用

# cotinue跳出本次循环, 继续执行;
# break: 跳出整个循环
tryCount = 0
while tryCount < 10:
    tryCount += 1
    if tryCount == 2:
        continue
    print(tryCount)


while死循环

# 1. 死循环的几种写法
while True:
     print('hello')
while 0<1:

     print("hello")

while 1:

    print("hello")

# 2.continue和break的区别
while True:
    cmd = input('Command:')
    if not cmd:
        continue
    elif cmd == 'q':
        break
    else:
        print('execute cmd %s' %(cmd))
# if "hello":
#     print("hello")
# else:
#     print("not hello")
# 3. if和while后面必须跟的是bool类型, 如果不是布尔类型,转化为bool类型
print(bool("hello"))   # True

print(bool(""))       # False


while循环猜数字游戏

# 猜数字游戏
# if , while, break
# 1. 系统随机生成一个1~100的数字;
# 2. 用户总共有5次猜数字的机会;
# 3. 如果用户猜测的数字大于系统给出的数字,打印“too big”;
# 4. 如果用户猜测的数字小于系统给出的数字,打印"too small";
# 5. 如果用户猜测的数字等于系统给出的数字,打印"恭喜中奖100万",
#并且退出循环;
# 注意while循环可以和else结合;

import random
sys_num = random.randint(1,50)
guess_count = 0
while guess_count < 3:
    # 转换接收的字符串类型为整形
    guess_num = int(input("Guess Num:"))
    # 猜测次数加1
    guess_count += 1
    # 判断
    if guess_num > sys_num:
        print('tooy big')
    elif guess_num < sys_num:
        print('too small')
    else:
        # break跳出循环;
        print("恭喜中奖100万")
        break
else:

    print("尝试次数超过3次")


for循环使用

#for(int i=0; i<10; i++){
#pass
#}
# range(end): 0~end-1
# range(start, end): start~end-1
# range(start, end, step): start~end-1, 步长为step
# range(1,10,2)  # 1, 1+2, 1+2+2,

for i in range(5):
    print(i)
else:

    print("结束执行....")



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值