python基础

CSDN话题挑战赛第2期icon-default.png?t=N7T8https://marketing.csdn.net/p/7b6697fd9dd3795a268d1a6f2fe75012
参赛话题:学习笔记


 print函数


1.使用逗号“,"隔开变量与其他剩余内容,则print在输出时会依次打印各个字符串和变量,遇到逗号时会输出一个空格

shot_id=2
print ('投篮的ID是',shot_id)
>>>投篮的ID是  2

2. print函数不仅可以打印变量值,也可以打印计算结果

shot_id = 2
print(shot_id)      #此处打印变量'shot_id'的值
print(shot_id+3)    #此处打印变量'shot_id'加上3的结果
print('shot_id+3=',shot_id+3)    #此行将等式打印出来的
>>>2
   5
   shot_id+3=5

•Python语言的数据类型包括整型int、浮点型float、字符串str、布尔型bool、复数complex和空型None。


整型(int)

整型的取值为为整数,有正有负,如2,-66,666等。


浮点型(float)shu

•浮点型的取值为小数,当计算有精确度要求时被使用,由于小数点可以在相应的二进制的不同位置浮动,故而称为浮点数

*如3.14,-6.66等,但是如果是非常大或者非常小的浮点数,就需要用科学计数法表示,用e 代替10。科比投球数据集中的lat和lon为浮点型变量。

print(3.14e9)
>>>3.140000000.0

字符串(str)

*字符串是两个单引号或两个双引号包裹起来的文本

*字符串Jump Shot包括J,u,m,p,空格,S,h,o,t这九个字符

print("Kobe's team name")
>>>Kobe's team name

*转义字符:字符串里常常存在一些如换行、制表符等有特殊含义的字符,这些字符称之为转义字符

*比如\n表示换行,\t表示制表符,Python还允许用r" "表示“ ” 内部的字符串默认不赚义

print("kobe's team name is Los Angeles Lakers.")

print("kobe's team name is \t Los Angeles Lakers.")

print("kobe's team name is \n  Los Angeles Lakers.")

print(r"kobe's team name is \n  Los Angeles Lakers.")


print("kobe's team name is Los Angeles Lakers.")
>>>kobe's team name is Los Angeles Lakers.
print("kobe's team name is \t Los Angeles Lakers.")
>>>kobe's team name is       Los Angeles Lakers.
print("kobe's team name is \n  Los Angeles Lakers.")
>>>kobe's team name is 
    Los Angeles Lakers.
print(r"kobe's team name is \n  Los Angeles Lakers.")
>>>kobe's team name is \n  Los Angeles Lakers.


布尔(Bool)

*布尔型只有True 和False两种值。比较运算和条件表达式都会产生True 或False.

shot_id=2
print("shot_id equals",shot_id)
print("'shot_id is more than 3' is",shot_id > 3)
print("'shot_id is less than 3' is",shot_id < 3)

>>>shot_id equals 2

       'shot_id is more than 3'  is False 

        'shot_id is leass than 3' is True 

     

*布尔值可以进行and 、 or 和not 运算,and 和or 运算分别用 &和 | 表示

*not运算为非运算,鸡巴True 变成False,False变成True.

print('True & False equals to',True&False)
print('Ture or False equals to',True|False)
print('not True equals to',not True)
print('not False equals to',not False)

>>>True & False equals to False 

>>>True or False equals to True

>>>not True equals to False

>>> not False equals to True


空值(None)

*空值是Python里一个特殊的值,用None表示,一般用None填充表格中的缺失值

*使用type()函数来获取某值是类型

print( type(2))
print(type(34.0443))
print(type('Jump Shot'))
print(type(True))
print(type(None))

>>> <type 'int'>

>>> <type 'float'>

>>> <type 'str'>

>>> <type 'bool'>

>>> <type 'NoneType'>


算数运算符

*二元数学运算符:

a+b        #a加b

a-b         #a减b

a*b        #a乘以b

a/b        #a除以b

a//b        #a除以b后向下圆整,丢弃小数部分

a**b        #a的b次方


数型转换

*函数int()、float()、str()和bool()分别用于将变量转换成整型、浮点型、字符串、和布尔型变量

print('shot_id is',type(shot_id))
print(tyoe(float(shot_id)),float(shot_id))
print(type(str(shot_id)),str(shot_id))
print(type(bool(shot_id)),bool(shot_id))

>>>shot_id is <type 'int'>

>>><type 'float'> 2.0

>>><type 'str'> 2

>>><type 'bool'> True

*某些变量无法转换成数值型变量

print('action_type is',type(action_type))

>>>action_type is <type 'str'>

print(type(int(action_type)),int(action_type))

ValueError                                 Traceback (most rencen call last)

    <ipython-input-24-512f5297a0e0> in <module>()

---------->1 print type(int(action_type)),int(action_type)
ValueError: invalid literal for int() with base 10:'Jump'

int('2')

>>>2

 

*只有在变量值为0时,bool转换的结果才为False:

print('action_type is',type(action_type))
print(type(bool(action_type)),bool(action_type))

>>>action_type is <type 'str'>

>>>type 'bool'> True

bool(0)

>>>False

*除了使用type()外,我们还可以使用isinstance()来获得数据类型

isinistance('2',str)
isinstance(2,int)

>>>True

>>>True

课堂感悟:

在第一阶段见习中,我认识了Pthon,逐步了解python ,对于python这门编程语言也越来越感兴趣,不再是作为门外汉对于python只有那抽象的映像,曾经我以为python语言非常高深,向我这样对编程对网络一无所知的小白那会是我一直触及不到领域。

在现阶段见习我们正在为python语言的学习打好基础,为接下来学习更高邻域的知识做好充足的准备。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

赶路者wt

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

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

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

打赏作者

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

抵扣说明:

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

余额充值