广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python文件类型,变量及字符串
  • 453
分享到

Python文件类型,变量及字符串

字符串变量文件类型 2023-01-31 03:01:40 453人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

1. 文件类型:(1)源代码:    vim test.py    #!/usr/bin/python    print 'hello world!'运行方法1:    [root@localhost Python]# python tes

1. 文件类型:

(1)源代码:

    vim test.py

    #!/usr/bin/python

    print 'hello world!'

运行方法1:

    [root@localhost Python]# python test.py

    hello world!

    [root@localhost python]#

运行方法2:

    [root@localhost python]# chmod +x test.py

    [root@localhost python]# ./test.py

    hello world!

    [root@localhost python]#

(2)字节代码:

python源文件编译后为扩展名字为.pyc

删除源码,编译后的二进制文件可以独立执行。

编译方法:

    vim 2.py

    #!/usr/bin/python

    import py_compile

    py_compile.compile('test.py')

    [root@localhost python]# python 2.py

    [root@localhost python]# ls

    2.py  test.py test.pyc

    [root@localhost python]#

    [root@localhost python]# python test.pyc

    hello world!

    [root@localhost python]#

(3)优化的代码:

    python -O -m py_compile test.py

    [root@localhost python]# python -O -m py_compile test.py

    [root@localhost python]# ls

    2.py  test.py  test.pyc test.pyo

    [root@localhost python]# python test.pyo

    hello world!

    [root@localhost python]#

2.Python的变量

变量是计算机内存中的一块区域,变量可以存储规定范围内的值,而且值可以改变结构。

python下变量是对一个数据的引用

(1)变量的命名:

    1.  变量名的长度不受限制,但其中的字符必须是字母、数字、或者下划线(_),而不能使用空格、连字符、标点符号、引号或其他字符。

    2. 变量名的第一个字符不能是数字,而必须是字母或下划线。

    3. Python区分大小写。

    4. 不能将Python关键字用作变量名。

  例如: a   a1   _a

(2)变量的赋值:

是变量的声明和定义的过程。

    a = 123

    In [1]: a = 123

    In [2]: a

    Out[2]: 123

    In [3]: id(a)

    Out[3]: 7891024

    In [4]: a = 456

    In [5]: id(a)

    Out[5]: 19127624

    In [6]:

(3)运算符和表达式:

    赋值运算符

    算术运算符

    关系运算符

    逻辑运算符

    

表达式:

    将不同的数据(包括变量、函数)用运算符号按一定的规则连接起来的一种式子。

1)赋值运算符

    In [68]: a = 3

    In [69]: a

    Out[69]: 3

    In [70]: a+=3

    In [71]: a

    Out[71]: 6

    In [72]: a-=4

    In [73]: a

    Out[73]: 2

    In [76]: a*=3

    In [77]: a

    Out[77]: 6

    In [78]: a/=2

    In [79]: a

    Out[79]: 3

    In [80]: a%=3

    In [81]: a

    Out[81]: 0

    In [82]:

2)算术运算符

    In [82]: 1 + 2

    Out[82]: 3

    In [83]: 2 - 1

    Out[83]: 1

    In [84]: 2 * 2

    Out[84]: 4

    In [85]: 6 / 2

    Out[85]: 3

    In [86]: 6 % 2

    Out[86]: 0

    In [88]: 3.999999 / 2

    Out[88]: 1.9999995

    In [89]: 3.999999 // 2

    Out[89]: 1.0

    In [90]: 3 ** 2

    Out[90]: 9

    In [91]:

3)关系运算符:

    In [91]: 1 > 2

    Out[91]: False

    In [92]: 2 < 3

    Out[92]: True

    In [93]: 2 >= 1

    Out[93]: True

    In [94]: 3 <= 56

    Out[94]: True

    In [95]: 3 == 3

    Out[95]: True

    In [96]: 2 != 34

    Out[96]: True

    In [97]:

4)逻辑运算符:

    In [97]: 1 < 2 and 2 > 0

    Out[97]: True

    In [98]: 1 == 1 and 2 < 1

    Out[98]: False

    In [99]: 1 == 1 or 2 < 1

    Out[99]: True

    In [100]: not 1 > 2

    Out[100]: True

5)各种运算符的优先级:

往右越高   上到下越高,

lambda 匿名函数。

blob.png

练习:

写一个四则运算器:

要求从键盘读取数字。

input()与raw_input()

    查看帮助:help(input)

    raw_input()都当然成字符串处理

    %s 格式化字符串。

    [root@localhost python]# cat 4.py

    #!/usr/bin/python

    num1 = input("Please input: ")

    num2 = input("Please input: ")

    print "%s + %s = %s" % (num1,num2,num1+num2)

    print "%s -  %s = %s" % (num1,num2,num1-num2)

    print "%s * %s = %s" % (num1,num2,num1*num2)

    print "%s / %s = %s" % (num1,num2,num1/num2)

    [root@localhost python]# python 4.py

    Please input: 3

    Please input: 5

    3 + 5 = 8

    3 -  5 = -2

    3 * 5 = 15

    3 / 5 = 0

    [root@localhost python]#


3.Python的数值和字符串

数据类型:

    数值

    字符串

    列表

    元组

    字典

(1)数值类型:

    整型

        In [6]: a = 123

        In [7]: type(a)

        Out[7]: int

        In [8]:

    长整型

        In [8]: a = 199999999999999999999999999999

        In [9]: a

        Out[10]: 199999999999999999999999999999L

        In [11]: type(a)

        Out[12]: long

        In [13]:

     浮点型

        0.0, 12.0   -18.8   3e+7等

        科学计数法是浮点型

        In [11]: 3e+7

        Out[11]: 30000000.0

        In [12]: type(3e+7)

        Out[12]: float

        In [13]: 3.0/2

        Out[13]: 1.5

        In [14]: type(3.0/2)

        Out[14]: float

        In [15]:

    复数型

        python对复数提供内嵌支持,这是大部分软件没有的。

        In [8]: a = 3.14j

        In [9]: a

        Out[9]: 3.14j

        In [10]: type(a)

        Out[10]: complex

(2)字符串类型:

        In [12]: a = 'abc'

        In [13]: a

        Out[13]: 'abc'

        In [14]: type(a)

        Out[14]: str

        In [15]:

        三重引号还可以做注释:.

        In [28]: a = 'hello\nworld'

        In [29]: a

        Out[29]: 'hello\nworld'

        In [30]: a = "hello\nworld"

        In [31]: a

        Out[31]: 'hello\nworld'

        In [39]: a = '''hello\nworld'''

        In [40]: a

        Out[40]: 'hello\nworld'

        In [41]: print a

        hello

        world

        In [42]:

        In [43]: type(a)

        Out[44]: str

    序列索引

        In [42]: a = 'abcde'

        In [43]: a[0]

        Out[43]: 'a'

        In [44]: a[1]

        Out[44]: 'b'

        In [45]: a[-1]

        Out[45]: 'e'

        In [46]: a[-2]

        Out[46]: 'd'

    序列切片:

        In [42]: a = 'abcde'

        In [43]: a[0]

        Out[43]: 'a'

        In [44]: a[1]

        Out[44]: 'b'

        In [45]: a[-1]

        Out[45]: 'e'

        In [46]: a[-2]

        Out[46]: 'd'

        In [47]: a[0:2]

        Out[47]: 'ab'

        In [48]: a[0:4]

        Out[48]: 'abcd'

        In [49]: a[0:3]

        Out[49]: 'abc'

        In [50]: a[1:3]

        Out[50]: 'bc'

        In [56]: a[0] + a[1]

        Out[56]: 'ab'

        In [57]: a[:2]

        Out[57]: 'ab'

        In [58]: a[:]

        Out[58]: 'abcde'

        In [59]: a[:-1]

        Out[59]: 'abcd'

        In [60]: a[::-1]

        Out[60]: 'edcba'

        In [61]: a[::1]

        Out[61]: 'abcde'

        In [62]: a[:3:1]

        Out[62]: 'abc'

        In [63]: a[::2]

        Out[63]: 'ace'

        In [64]: a

        Out[64]: 'abcde'

        In [65]: a[-4::-2]

        Out[65]: 'b'

        In [66]: a[-4:-2]

        Out[66]: 'bc'

        In [67]: a[-2:-4:-1]

        Out[67]: 'dc'

        In [68]:

练习:

1.将 “123” 转换成整数

    In [10]: a = int(123)

    In [11]: print a

    123

    In [12]:

2.将 “9999999999999999999” 转换成长整数

    In [18]:  a = long(9999999999999999999)

    In [19]: print a

    9999999999999999999

    In [20]: a

    Out[20]: 9999999999999999999L

    In [21]:

3.将 “3.1415926” 转换成一个浮点数

    In [21]:  a = float(3.1415926)

    In [22]: print a

    3.1415926

    In [23]:

4.将 123 转换成一个字符串

    In [23]:  a = str(123)

    In [24]: print a

    123

    In [25]:

5.现有以下字符串

字符串1:" abc deFGh&*ijkl opq mnrst((uvwxyz "

字符串2:" ABC#DEF GH%IJ MNOPQ KLRS&&TUVWX(*&YZ "

使用字符串的各种方法转换成如下方式

ABCDEFGHIJKLMNOPQRSTUVWXYZzyxwvutsrqponmlkjihgfedcba

    In [25]: str1 = " abc deFGh&*ijkl opq mnrst((uvwxyz "

    In [26]: str2 = " ABC#DEF GH%IJ MNOPQ KLRS&&TUVWX(*&YZ "

    In [27]: print filter(lambda x:x.isalpha(),str2+str1[::-1].lower())

    ABCDEFGHIJMNOPQKLRSTUVWXYZzyxwvutsrnMQpolkjihgfedcba

    In [28]:



--结束END--

本文标题: Python文件类型,变量及字符串

本文链接: https://www.lsjlt.com/news/187444.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Python文件类型,变量及字符串
    1. 文件类型:(1)源代码:    vim test.py    #!/usr/bin/python    print 'hello world!'运行方法1:    [root@localhost python]# python tes...
    99+
    2023-01-31
    字符串 变量 文件类型
  • python字符串不可变数据类型
    目录一、截取子串-切片二、查找子串及数量三、字符串的替换、分割以及合并四、字母的大小写五、删除侧边的空白六、对齐方式七、判断首位字符是否正确(返回布尔值)八、判断字母、数以及空格一、...
    99+
    2022-11-13
  • Python入门基础之变量及字符串
    目录变量关于变量变量名命名规则字符串原始字符串长字符串总结变量 当把一个值赋给一个名字时,它就会存储在内存中,我们把这块内存称为变量(variable)。 在大多数语言中,都把这种行...
    99+
    2022-11-12
  • PHP格式、数据类型、常量及字符串
    PHP脚本以结束。 ...
    99+
    2023-09-03
    php 开发语言
  • Python字符串类型及格式化问题
    目录一、字符串类型二、字符串类型三、字符串的索引四、字符串的切片五、format()方法的基本使用六、format()方法的格式控制总结一、字符串类型 1)字符串是字符的序列表示,根...
    99+
    2023-02-21
    Python字符串类型 Python格式化 Python字符串格式化
  • Python变量类型及变量引用
    二、变量的类型        Python 不包含像 int 这样的简单类型 —— 只有对象类型, 如果 Python 中需要整数值,将整数赋值给相应变量(如i = 100 )即可。在后台,Python 将创建一个整数对象,并将对新对象的引...
    99+
    2023-01-31
    变量 类型 Python
  • Python变量和字符串详解
    几个月前,我开始学习个人形象管理,从发型、妆容、服饰到仪表仪态,都开始做全新改造,在塑造个人风格时,最基础的是先了解自己属于哪种风格,然后找到参考对象去模仿,可以是自己欣赏的人、明星或模特等,直至最后去创新...
    99+
    2022-06-04
    字符串 变量 详解
  • python变量和字符串(笔记)
    1、变量名就像我们现实社会的名字,把一个值赋值给一个名字时,它会存储在内存中,称之为变量,大多数语言中,都把这种行为成为“给变量赋值”或“把值存储在变量中”。2、不过python与大多数其他计算机语言的做法稍有不同,它并不是把值存储在变量中...
    99+
    2023-01-31
    字符串 变量 笔记
  • 如何分析Python的数据类型、变量、字符串和格式化
    这篇文章主要为大家分析了如何分析Python的数据类型、变量、字符串和格式化的相关知识点,内容详细易懂,操作细节合理,具有一定参考价值。如果感兴趣的话,不妨跟着跟随小编一起来看看,下面跟着小编一起深入学习“如何分析Python的数据类型、变...
    99+
    2023-06-29
  • python如何将字符类型、数值类型等转换为字符串类型
    这篇文章主要为大家展示了“python如何将字符类型、数值类型等转换为字符串类型”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“python如何将字符类型、数值类...
    99+
    2022-10-19
  • python怎么判断字符串类型
    在Python中,可以使用type()函数来判断字符串的类型。例如:```pythonstring1 = "Hello, world...
    99+
    2023-09-16
    python
  • Python 变量教程字节对象与字符串
    目录前言编码解码前言 在 Python 2 中,str 和 bytes 都是相同的 typeByte 对象,而在 Python 3 中,Byte 对象在 Python 3 中定义为&...
    99+
    2022-11-11
  • 利用TypeScript从字符串字面量类型提取参数类型
    目录正文挑战需要掌握的内容字符串字面量类型模板字面量类型和字符串字面量类型条件类型函数重载和通用函数着手解决问题分割字符串字面量类型参数语法部分的过滤在对象类型里做一个映射正文 挑战...
    99+
    2022-11-13
  • python如何在字符串中插入变量
    共三个用法,如下所示 使用字符串的format()函数,通过" 字{0}符{1}串".format(变量1,变量2,...)的方式,所有变量都作为format()函数的参数,在字符串中用“{}”来接收...
    99+
    2023-09-29
    python 开发语言
  • Python字节串类型bytes及用法
    一、bytes 字节串类型概述 Python 3 新增了 bytes 类型,用于代表字节串,是一个类型,不是C#中的列表。 由于 bytes 保存的就是原始的字节...
    99+
    2022-11-11
  • Python高级变量之字典和字符串详解
    目录1、字典的定义字典和列表的区别:字典的基本使用2、循环遍历3、字符串的定义4、字符串的常用操作字符串 查找和替换字符串 文本对齐演练去除空白字符字符串拆分和连接5、字符串的切片总...
    99+
    2022-11-12
  • Python数据类型详解(一)字符串
    一.基本数据类型   整数:int   字符串:str(注:t等于一个tab键)   布尔值: bool   列表:list   列表用[]   元祖:tuple   元祖用()   字典:dict ...
    99+
    2022-06-04
    字符串 详解 数据类型
  • Python标准数据类型-String(字符串)
    ✅作者简介:CSDN内容合伙人、阿里云专家博主、51CTO专家博主、新星计划第三季python赛道Top1 📃个人主页:hacker707的csdn博客 🔥系列专栏...
    99+
    2023-09-03
    python 字符串 原力计划
  • Python字符串类型及格式化问题怎么解决
    这篇文章主要讲解了“Python字符串类型及格式化问题怎么解决”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Python字符串类型及格式化问题怎么解决”吧!一、字符串类型1)字符串是字符的序...
    99+
    2023-07-05
  • Python 数据类型中的字符串和数字
    目录一、变量1.变量2.变量的命名规则二、标准数据类型1.字符串字符串(string)转义字符修改字符串大小写删除字符串空白判断字符串全是字母或数字字符串查找字符串替换2.数字(Nu...
    99+
    2022-11-13
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作