资讯专栏INFORMATION COLUMN

python学习笔记1---python的基本数据类型

mgckid / 2502人阅读

摘要:数字整型与浮点型整数没有之分浮点数里面没有单精度和双精度之分表示整除进制进制,进制,进制,进制,,,。。。。

Number:数字 1)整型与浮点型
整数:int(没有short、int、long之分)
浮点数:float(python里面没有单精度和双精度之分)
>>> print("hello world")
hello world
>>> 1
1
>>> 133434
133434
>>> type(1)

>>> type(-1)

>>> type(1.1)

>>> type(1.111111111111111111)

>>> type(1+0.1)

>>> type(1+1)

>>> type(1+1.0)

>>> type(1*1)

>>> type(1*1.0)

>>> type(2/2)

>>> type(2//2)

>>> 2/2
1.0
>>> 2//2
1
>>> 1//2  //表示整除
0
2)10、2、8、16进制
>>> 10进制,2进制,8进制,16进制
SyntaxError: invalid character in identifier
>>> 0,1,2,3。。。。9,10
SyntaxError: invalid character in identifier
>>> 0,1,10
SyntaxError: invalid character in identifier
>>> 0,1。。。。7,10
SyntaxError: invalid character in identifier
>>> 0,1,2.。。。。。。。9,A,B,C,D,E,F
SyntaxError: invalid character in identifier
表示方法:
二进制(0b10)
八进制(0o10)
十六进制(0x10)
>>> 0b10
2
>>> 0b11
3
>>> 0o10
8
>>> 0o11
9
>>> 0x10
16
>>> 0x11
17
进制转换

转换为二进制

>>> bin(10)
"0b1010"
>>> bin(0o7)
"0b111"
>>> bin(0xE)
"0b1110"

转换为十进制

>>> int(0b111)
7
>>> int(0o77)
63

转换为十六进制

>>> hex(888)
"0x378"
>>> hex(0o7777)
"0xfff"

转换为八进制

>>> oct(0b111)
"0o7"
>>> oct(0x777)
"0o3567"
3)bool 布尔类型(数字类型的一种):表示真、假
>>> True
True
>>> False
False
>>> true
Traceback (most recent call last):
  File "", line 1, in 
    true
NameError: name "true" is not defined
>>> false
Traceback (most recent call last):
  File "", line 1, in 
    false
NameError: name "false" is not defined
>>> type(True)

>>> type(False)

>>> int(True)
1
>>> int(False)
0
>>> bool(1)
True
>>> bool(0)
False
>>> bool(2)
True
>>> bool(-1.1)
True
>>> bool(0b01)
True
>>> bool(0b0)
False
>>> bool("abc")
True
>>> bool("")
False
>>> bool([1,2,3])
True
>>> bool([])
False
>>> bool({1,1,1})
True
>>> bool({})
False
>>> bool(None)
False
4)complex 复数
>>> 36j
36j
str 字符串 1)单引号
>>> "hello world"
"hello world"
>>> "let"s go"
"let"s go"
2)双引号
>>> "hello world"
"hello world"
>>> "let"s go"
SyntaxError: invalid syntax
>>> "let"s go"
"let"s go"
3)三引号(多行字符串)
>>> """
hello world
hello world
hello world
"""
"
hello world
hello world
hello world
"
>>> """
hello world
hello world
hello world
"""
"
hello world
hello world
hello world
"
>>> """hello world
hello world
hello world"""
"hello world
hello world
hello world"
>>> print("""hello world
hello world
hello world""")
hello world
hello world
hello world
>>> print("""hello world
hello world
hello world""")
hello world
hello world
hello world
>>> print("hello world
hello world
hello world")
hello world
hello world
hello world
>>> """hello world
hello world"""
"hello world
hello world"
>>> "hello
SyntaxError: EOL while scanning string literal
>>> "hello
world"
"helloworld"
4)转义字符(特殊的字符)
无法“看见”的字符
与语言本身语法有冲突的字符

n 换行

" 单引号

t 横向制表符

n 换行

r 回车

>>> print("hello 
 world")
hello 
 world
>>> print(""hello 
 world"")
"hello 
 world"
>>> print(""hello 
 world"")
"hello 
 world"
>>> print("hello 
 world")
hello 
 world
>>> print("c:
orthwind
orthwest")
c:
orthwind
orthwest
>>> print("c:
orthwind
orthwest")
c:
orthwind
orthwest
>>> print(r"c:
orthwind
orthwest")
c:
orthwind
orthwest
>>> r"C:Windows"
"C:Windows"
>>> R"C:Windows"
"C:Windows"
//字符串前加上“r”以后就不是一个普通字符串,而是一个原始字符串
>>> print(r" let "s go")
      
SyntaxError: invalid syntax
//这里的单引号不是成对出现,已经不是一个字符串,加上“r”也不会是原始字符串
字符串运算 一
>>> "hello"
"hello"
>>> "world"
"world"
>>> "hello world"
"hello world"
>>> "hello"+"world"
"helloworld"
>>> "hello"*3
"hellohellohello"
>>> "hello" * "world"
Traceback (most recent call last):
  File "", line 1, in 
    "hello" * "world"
TypeError: can"t multiply sequence by non-int of type "str"
>>> "hello world"[0]
"h"
>>> "hello world"[3]
"l"
>>> "hello world"[4]
"o"
>>> "hello world"[5]
" "
>>> "hello world"[-1]
"d"
>>> "hello world"[-3]
"r"
//[-n]表示从字符串的末尾往前数n次得到的字符
字符串运算 二
>>> "hello world"[6]
"w"
>>> "hello world"[-5]
"w"
>>> "hello world"[0:4]
"hell"
>>> "hello world"[0:5]
"hello"
>>> "hello world"[0:-1]
"hello worl"
//这里的-1表示步长
>>> "hello world"[0:-3]
"hello wo"
字符串运算 三
>>> "hello world"[6:10]
"worl"
>>> "hello world"[6:11]
"world"
>>> "hello world"[6:20]
"world"
>>> "hello world"[6:-1]
"worl"
>>> "hello world"[6:0]
""
>>> "hello world"[6:-0]
""
>>> "hello world"[6:]
"world"
>>> "hello python java c# javascript php ruby"[6:]
"python java c# javascript php ruby"
>>> "hello python java c# javascript php ruby"[:-4]
"hello python java c# javascript php "
>>> "hello python java c# javascript php ruby"[0:-4]
"hello python java c# javascript php "
>>> "hello python java c# javascript php ruby"[-4:]
"ruby"

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/44770.html

相关文章

  • python3学习笔记(1)----基本语法

    摘要:一的基本语法缩进统一个或者个空格。中的数据类型中有个标准类型数字字符串列表元组集合字典数字复数在中,只有一种整数类型,表示长整型。如则会显示,并不是换行。空行与代码缩进不同,空行并不是语法的一部分。我们将首行及后面的代码组称为一个子句。 一、python3的基本语法 1、缩进统一(1个tab或者4个空格)。 for i in range(10): print (i) ...

    yanwei 评论0 收藏0
  • python学习笔记02-数据类型

    摘要:基本数据类型基本数据类型比较简单,通过以下例子演示运行结果如下通用序列操作索引通过索引获取序列的单个元素,也可以使用负数索引。设置参数步长,负数步长表示从右侧开始提取元素。注意相同类型的序列才可以进行连接操作。 showImg(https://segmentfault.com/img/bV09Mw?w=805&h=327); 0. 基本数据类型 基本数据类型比较简单,通过以下例子演示:...

    jcc 评论0 收藏0
  • python3学习笔记(2)----python数据类型

    摘要:的基本数据类型中的变量不需要声明。在里,只有一种整数类型,表示为长整型,没有中的。字符串的截取的语法格式如下变量头下标尾下标索引值以为开始值,为从末尾的开始位置。列表列表是中使用最频繁的数据类型。注意构造包含或个元素的元组的特殊语法规则。 1、python3的基本数据类型 Python 中的变量不需要声明。每个变量在使用前都必须赋值,变量赋值以后该变量才会被创建。在 Python 中,...

    陆斌 评论0 收藏0
  • 第7期 Datawhale 组队学习计划

    马上就要开始啦这次共组织15个组队学习 涵盖了AI领域从理论知识到动手实践的内容 按照下面给出的最完备学习路线分类 难度系数分为低、中、高三档 可以按照需要参加 - 学习路线 - showImg(https://segmentfault.com/img/remote/1460000019082128); showImg(https://segmentfault.com/img/remote/...

    dinfer 评论0 收藏0
  • 重磅 | 完备 AI 学习路线,最详细资源整理!

    摘要:是你学习从入门到专家必备的学习路线和优质学习资源。的数学基础最主要是高等数学线性代数概率论与数理统计三门课程,这三门课程是本科必修的。其作为机器学习的入门和进阶资料非常适合。书籍介绍深度学习通常又被称为花书,深度学习领域最经典的畅销书。 showImg(https://segmentfault.com/img/remote/1460000019011569); 【导读】本文由知名开源平...

    荆兆峰 评论0 收藏0

发表评论

0条评论

mgckid

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<