基础的数据结构:int, float, string

注意:python入门系列的文章的示例均使用python3来完成。

#!/bin/python

a = 1
b = 1.0
c = 'string' print(a, type(a))
print(b, type(b))
print(c, type(c))

输出结果:

1 <class 'int'>
1.0 <class 'float'>
string <class 'str'>

type(a)这个表示获取a变量所属的类型,可以看到a,b,c分别属于int,float,str类型。

注意,type(a)返回的是对应的class名称,而不是简单的字符串类型。说明在python3中,所有的类型都归属于class。

测试type的返回值

#!/bin/python

a = 1

print(a, type(a))
print(type(a) == 'int')
print(type(a) == int)

输出结果:

1 <class 'int'>
False
True

引入bool类型:只有连个值,是常量的bool值,True和False,注意这里是区分大小写的。True不能写成TRUE,也不能写成true。

代码实例:

#!/bin/python

a = True
print(a, type(a))
print(type(a) == bool) b = False
print(b, type(b))
print(type(b) == bool)

输出:

True <class 'bool'>
True
False <class 'bool'>
True

int,float,str互转

示例代码

#!/bin/python

a = 1
print(str(a), type(str(a)))
print(float(a), type(float(a))) b = 1.0
print(str(b), type(str(b)))
print(int(b), type(int(b))) c = "1.0"
print(float(c), type(float(c)))
print(int(float(c)), type(int(float(c))))

输出结果:

1 <class 'str'>
1.0 <class 'float'>
1.0 <class 'str'>
1 <class 'int'>
1.0 <class 'float'>
1 <class 'int'>

以上是:int,float,str三者的互转。

python中如何添加注释

#!/bin/python

#It is a comment

'''
This is a comment;
thie is another comment
'''

分两种注释:#表示注释当前行,'''表示注释多行

【python】入门指南1的更多相关文章

  1. Python入门指南(超详细)

    Python 是一门非常容易上手的语言,通过查阅资料和教程,也许一晚上就能写出一个简单的爬虫.但 Python 也是一门很难精通的语言,因为简洁的语法背后隐藏了许多黑科技.本文主要针对的读者是: 毫无 ...

  2. Python 入门指南

    Release: 3.4 Date: March 29, 2014 Python 是一门简单易学且功能强大的编程语言. 它拥有高效的高级数据结构,并且能够用简单而又高效的方式进行面向对象编程. Pyt ...

  3. 25 【python入门指南】如何编写测试代码

    python如何编写测试代码 python内置了unittest,使得写应用层的单元测试变得超乎寻常的简单. 1,执行单个测试函数 #!/bin/python import unittest clas ...

  4. 24 【python入门指南】class

    一.类 1.1,构造函数,析构函数 #!/bin/python class dog(): def __init__(self, age, name): self.age = age self.name ...

  5. python入门教程链接

    python安装 选择 2.7及以上版本 linux: 一般都自带 windows: https://www.python.org/downloads/windows/ mac os: https:/ ...

  6. Python入门一:简单得不能再简单了##

    从python的语法上看,简单得不能再简单了. 想学它,请移步廖雪峰python2.7教程以及python3.这实在是最好的入门教程.参考资料太多: 外国的教程 Python 入门指南 Python ...

  7. Python不完全入门指南

    适用范围: 有一定编程基础,想快速入门python的人群 说明: 使用jupyter notebook编写,可以使用nbviewer网站进行查看. Python不完全入门指南 项目放在github上, ...

  8. Python 30分钟入门指南

    Python 30分钟入门指南 为什么 OIer 要学 Python? Python 语言特性简洁明了,使用 Python 写测试数据生成器和对拍器,比编写 C++ 事半功倍. Python 学习成本 ...

  9. Python 30分钟快速入门指南

    学习地址 中文版:Python 30分钟入门指南 英文版:Learn X in Y minutes 学习时间 2019/03/10 19:00 - 19:32,多用了2分钟.

  10. Python 极速入门指南

    前言 转载于本人博客. 面向有编程经验者的极速入门指南. 大部分内容简化于 W3School,翻译不一定准确,因此标注了英文. 包括代码一共两万字符左右,预计阅读时间一小时. 目前我的博客长文显示效果 ...

随机推荐

  1. Spring boot 定制自己的错误

    1).如何定制错误的页面: ​ 1).有模板引擎的情况下:error/状态码; [将错误页面命名为 错误状态码.html 放在模板引擎文件夹里面的 error文件夹下],发生此状态码的错误就会来到 对 ...

  2. 模板引擎Dot

    Dot.js 很轻,处理速度也快,作为将json数据赋值到html页面的最好帮手. html5新引入的<template></template>就不用原先的<script ...

  3. mybatisGenerator自动生成pojo、dao、xml文件

    一.简介: mybatisGenerator是一款自动生成文件工具.本文使用idea2017.3.1来进行操作. 二.文件生成之后的项目结构: 三.开始生成步骤: 1.使用idea生成maven的结构 ...

  4. kafka无法消费数据

    遇到一个问题,使用Python kafka客户端和kafka命令行都无法消费数据,但是在kafka命令行后面添加--partition 0后就可以消费数据. bin/kafka-console-con ...

  5. 尚硅谷springboot学习18-日志使用

    默认配置 SpringBoot默认帮我们配置好了日志 //记录器 Logger logger = LoggerFactory.getLogger(getClass()); @Test public v ...

  6. oracle第二天笔记

    多表查询 /* 多表查询: 笛卡尔积: 实际上是两张表的乘积,但是在实际开发中没有太大意义 格式: select * from 表1,表2 */ select * from emp; select * ...

  7. 打包制作 ANE

    一.打包ANE 1.ios 准备文件: anePackager.bat aneswc.swc extension.xml flashAne.ane ioslib.a library.swf platf ...

  8. jsp 获取服务器ip 以及端口号

    <a href=<%="http://"+request.getLocalAddr()+":"+request.getLocalPort()+&qu ...

  9. Haskell语言学习笔记(91)Comprehension Extensions

    Comprehension Extensions 关于解析式的相关语言扩展. List and Comprehension Extensions 24 Days of GHC Extensions: ...

  10. Android文档 学习目录

    Building Your First App After you've installed the Android SDK, start with this class to learn the b ...