Python笔记 #01# Convert Python values into any type
源:DataCamp
datacamp 的 DAILY PRACTICE + 日常收集。
How much is your $100 worth after 7 years?
convert Python values into any type
Which one of these will throw an error?
How much is your $100 worth after 7 years?
Suppose you have $100, which you can invest with a 10% return each year. After one year, it's 100×1.1=110100×1.1=110 dollars, and after two years it's 100×1.1×1.1=121100×1.1×1.1=121. Add code on the right to calculate how much money you end up with after 7 years.
# How much is your $100 worth after 7 years?
print(100 * 1.1 ** 7)
Guess the type
Suppose you've defined a variable a, but you forgot the type of this variable.
In [1]: type(a)
Out[1]: float In [2]: type(b)
Out[2]: str In [3]: type(c)
Out[3]: bool In [4]:
Convert Python values into any type
# Definition of savings and result
savings = 100
result = 100 * 1.10 ** 7 # Fix the printout
print("I started with $" + str(savings) + " and now have $" + str(result) + ". Awesome!") # Definition of pi_string
pi_string = "3.1415926" # Convert pi_string into float: pi_float
pi_float = float(pi_string)
Which one of these will throw an error?
In [1]: "I can add integers, like " + str(5) + " to strings."
Out[1]: 'I can add integers, like 5 to strings.' In [2]: "I said " + ("Hey " * 2) + "Hey!"
Out[2]: 'I said Hey Hey Hey!' In [3]: True + False
Out[3]: 1 In [4]: "The correct answer to this multiple choice exercise is answer number " + 2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
"The correct answer to this multiple choice exercise is answer number " + 2
TypeError: Can't convert 'int' object to str implicitly
Python笔记 #01# Convert Python values into any type的更多相关文章
- Python网络01 原始Python服务器
原文:Python网络01 原始Python服务器 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 之前我的Python教程中有人 ...
- Python应用01 原始Python服务器
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 之前我的Python教程中有人留言,表示只学Python没有用,必须学会一个框架( ...
- Python笔记·第一章—— Python基础(一)
一.Python的简介 1.Python的由来与版本 1.1 python的由来 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆(中文 ...
- python笔记01:基础知识
1.4 数字和表达式 # -*- coding:utf-8 -*- #1.4 #除法 print 1 / 2 print 1.0 / 2 print 10 / 3 print 10.0 / 3.0 p ...
- 【python学习笔记01】python的数据类型
python的基本数据类型 整型 int 浮点型 float 真值 bool 字符串 str 列表 list #[1,2,3] 元组 tuple #(1,2,3) 字典 dict ...
- Linux 笔记 #01# 搭建 Python 环境 & vim 代码高亮
日常收集 vim editor: How do I enable and disable vim syntax highlighting? 搭建 Python 环境 vim editor: How d ...
- python笔记01
一.编码: (一)编码分类 ①ascii编码: 占有1个字节,8位,可表示2**8个字符,(py2默认编码方式). ②unicode: 万国码,占有4个字节,32位,可表示2**32个字符. ③utf ...
- python笔记01(详情请看廖雪峰的官方网站)
python 在调用函数的时候, 如果传入的参数数量不对, 如果传入的参数类型不对 会报TypeError的错误,并且Python会明确提示参数错误原因. hex()内置函数会把一个整数转换成十六进制 ...
- python笔记-01
Python环境安装 1.Windows下一键环境安装包 2.Python2.X与Python3.X 什么是代码? 代码是现实世界事物在计算机世界中的映射 什么是写代码? 写代码是将现实世界中的事物用 ...
随机推荐
- 动态规划——最长公共子序列&&最长公共子串
最长公共子序列(LCS)是一类典型的动归问题. 问题 给定两个序列(整数序列或者字符串)A和B,序列的子序列定义为从序列中按照索引单调增加的顺序取出若干个元素得到的新的序列,比如从序列A中取出 A ...
- Codeforces Round #186 (Div. 2).D
纠结的一道dp. 状态转移方程还是比较好想的,优化比较纠结 D. Ilya and Roads time limit per test 3 seconds memory limit per test ...
- 1853: [Scoi2010]幸运数字[容斥原理]
1853: [Scoi2010]幸运数字 Time Limit: 2 Sec Memory Limit: 64 MBSubmit: 2405 Solved: 887[Submit][Status] ...
- Android开源项目分类汇总【畜生级别】
From :http://blog.csdn.net/forlong401/article/details/25459403?c=6c4cd677a617db4655988e41ee081691#t7 ...
- Maven开发系统
Maven的优点: 自动从互联网中获取jar包,并实现了一步构建. pom.xml的配置 依赖管理(导入对应的jar包) 通过坐标(定位到仓库中的包的位置,并将jar包导入到项目中,如果版本升级,只需 ...
- nginx提高加载静态文件速度
1.本来对于静态网页,我们不需要放在应用容器中,原因一时由于应用服务器是用来解析动态网页的,针对静态网页本来就性能不高,而且还会占用应用容器的资源,所以我们专门使用nginx用来解析静态网页. ...
- 160316、实时处理oracle数据库中表的数据变化
http://blog.csdn.net/as339000204/article/details/45390727 近期接受项目需求,需要实时处理oracle数据库中表的数据变化,首先想到的是 ...
- 服务端使用Zookeeper注册服务地址,客户端从Zookeeper获取可用的服务地址。
一个轻量级分布式RPC框架--NettyRpc - 阿凡卢 - 博客园 http://www.cnblogs.com/luxiaoxun/p/5272384.html 这个RPC框架使用的一些技术所解 ...
- 01. Java序列化与反序列化简介
Java对象的序列化与反序列化 ; 给大家讲解一下什么是序列化 & 反序列化 当两个进程进行远程通讯的时候,彼此相互可以发送各种类型的数据,如文本,图片,语音和视频等无论是任何类型,最终都会 ...
- jquery的常用知识点
一.用jquery寻找元素 1.选择器 基本选择器: $("*") $("#id") 用id匹配 $(".class") 用class名匹配 ...