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 什么是代码? 代码是现实世界事物在计算机世界中的映射 什么是写代码? 写代码是将现实世界中的事物用 ...
随机推荐
- vue2.0非父子间进行通讯
在vue中,父组件向之组件通讯使用的是props,子组件向父组件通讯使用的是$emit+事件,那非父子间的通讯呢,在官方文档上只有寥寥数笔, 概念很模糊,这个空的vue实例应该放在哪里呢,光放文档并没 ...
- 『SharePoint 2010』Sharepoint 2010 Form 身份认证的实现(基于SQL)
1:创建一个基于身份认证的应用程序(具体参见上篇基于AD) SQL-MembershipProvider 成员SQL-RoleManager 角色 2:修改管理中心,我们创建的应用程序,还有Web服务 ...
- 《从零开始学Swift》学习笔记(Day 64)——Cocoa Touch设计模式及应用之目标与动作
原创文章,欢迎转载.转载请注明:关东升的博客 目标(Target)与动作(Action)是iOS和OS X应用开发的中事件处理机制. 问题提出 如图所示是一个ButtonLabelSample案例 ...
- 160309、Spring AOP操作action时无法注入,报空指针错误
今天帮同事看个问题,action注入失败,代码没问题,主要是stuts2权限移交的问题,特此记录一下 Spring AOP操作action时无法注入,报NullPointer异常 当使用Spring ...
- 按批次处理list数据 (list按条数取)
按批次处理list数据的两种方法 主要应用于list存储数据过多,不能使list整体进行其余操作 Java | 复制 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...
- android系统权限SET_PREFERRED_APPLICATIONS怎么获取
最近自己写个demo,需要用到SET_PREFERRED_APPLICATIONS权限 在网上找了半天,大概有两种方式: 1. 将手机root后,把apk强行push到system/app目录下,重启 ...
- wiki配置文件
jira数据库配置文件(链接:https://blog.csdn.net/jiangguilong2000/article/details/39718407) /var/atlassian/appli ...
- 5.2 - ToDoList
一.ToDoList需求 参考链接http://www.todolist.cn/ 1.将用户输入添加至待办项 2.可以对todolist进行分类(待办项和已完成组),用户勾选既将待办项分入已完成组 3 ...
- qemu网络虚拟化之数据流向分析二
2016-09-27 上篇文章大致介绍了qemu网络虚拟化相关的数据结构,本篇就结合qemu-kvm源代码分析下各个数据结构是如何初始化以及建立联系的. 这里还是分为三个部分: 1.Tap设备区 2. ...
- Python并行编程(二):基于线程的并行
1.介绍 软件应用中使用最广泛的并行编程范例是多线程.通常一个应用有一个进程,分成多个独立的线程,并行运行.互相配合,执行不同类型的任务. 线程是独立的处理流程,可以和系统的其他线程并行或并发地执行. ...