源:DataCamp

datacamp 的 DAILY PRACTICE  + 日常收集。

How much is your $100 worth after 7 years?

Guess the type

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的更多相关文章

  1. Python网络01 原始Python服务器

    原文:Python网络01 原始Python服务器 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 之前我的Python教程中有人 ...

  2. Python应用01 原始Python服务器

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 之前我的Python教程中有人留言,表示只学Python没有用,必须学会一个框架( ...

  3. Python笔记·第一章—— Python基础(一)

    一.Python的简介 1.Python的由来与版本 1.1 python的由来 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆(中文 ...

  4. 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 ...

  5. 【python学习笔记01】python的数据类型

    python的基本数据类型 整型 int 浮点型 float 真值 bool 字符串 str 列表 list       #[1,2,3] 元组 tuple    #(1,2,3) 字典 dict   ...

  6. Linux 笔记 #01# 搭建 Python 环境 & vim 代码高亮

    日常收集 vim editor: How do I enable and disable vim syntax highlighting? 搭建 Python 环境 vim editor: How d ...

  7. python笔记01

    一.编码: (一)编码分类 ①ascii编码: 占有1个字节,8位,可表示2**8个字符,(py2默认编码方式). ②unicode: 万国码,占有4个字节,32位,可表示2**32个字符. ③utf ...

  8. python笔记01(详情请看廖雪峰的官方网站)

    python 在调用函数的时候, 如果传入的参数数量不对, 如果传入的参数类型不对 会报TypeError的错误,并且Python会明确提示参数错误原因. hex()内置函数会把一个整数转换成十六进制 ...

  9. python笔记-01

    Python环境安装 1.Windows下一键环境安装包 2.Python2.X与Python3.X 什么是代码? 代码是现实世界事物在计算机世界中的映射 什么是写代码? 写代码是将现实世界中的事物用 ...

随机推荐

  1. java后台高德经纬度转地理位置信息

    import java.util.LinkedList;import java.util.List; import org.apache.http.HttpResponse;import org.ap ...

  2. 一个网络设备的常见功能--连通性检查SSRF漏洞--被黑客利用当做扫描器

    一.我们先来看一下很多网络设备都有的一个常见功能--连通性测试: 很多网络设备都具备与其他设备通信,联动的功能,例如网络设备联动安全设备,网络设备联动认证设备等等.此时都需要一个对端IP和对端端口号作 ...

  3. iOS 按钮文字加划掉线

    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(, , , )]; NSDictionary *normalTitleAttribu ...

  4. 类似hibernate实现sql增删改错

    Util package utils; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Ha ...

  5. java nio探险

    区别于io: nio是基于通道和缓冲区的,io是基于字节流和字符流的,(千万别被这些破名词唬住).以读取文件为例,文件就是自来水厂,通道就是自来水管道,缓冲区就是你家的缸(或者盛水的xx容器,例如你的 ...

  6. eclipse中设置在编译运行项目之前自动保存修改的文件

    Window -> Preferences -> General -> Workspace -> “Save automatically before build” Windo ...

  7. poj3233—Matrix Power Series

    题目链接:http://poj.org/problem?id=3233 题目意思:给一个矩阵n*n的矩阵A和一个k,求一个式子 S = A + A2 + A3 + … + Ak. 这个需要用到等比数列 ...

  8. 并发编程 - 协程 - 1.协程概念/2.greenlet模块/3.gevent模块/4.gevent实现并发的套接字通信

    1.协程并发:切+保存状态单线程下实现并发:协程 切+ 保存状态 yield 遇到io切,提高效率 遇到计算切,并没有提高效率 检测单线程下 IO行为 io阻塞 切 相当于骗操作系统 一直处于计算协程 ...

  9. 用hashlib生成动态token

    生成token: import timeimport hashlib token ='jdhfvasuiodfbhsjdbvaio' client_time = time.time() client_ ...

  10. 七个可以提升python程序性能的好习惯,你知道吗?

    掌握一些技巧,可尽量提高Python程序性能,也可以避免不必要的资源浪费.今天就为大家带来七个可以提升python程序性能的好习惯,赶快来学习吧:. 1.使用局部变量 尽量使用局部变量代替全局变量:便 ...