DataCamp的Introduction to Python之Python Basics是一些比较基础的练习,可以很快掌握。

The Python Interface

In the Python script on the right, you can type Python code to solve the exercises. If you hit Run Code or Submit Answer, your python script (script.py) is executed and the output is shown in the IPython Shell. Submit Answer checks whether your submission is correct and gives you feedback.

You can hit Run Code and Submit Answer as often as you want. If you’re stuck, you can click Get Hint, and ultimately Get Solution.

You can also use the IPython Shell interactively by simply typing commands and hitting Enter. When you work in the shell directly, your code will not be checked for correctness so it is a great way to experiment.

Instructions

  • Experiment in the IPython Shell; type 5 / 8, for example.
  • Add another line of code to the Python script on the top-right (not in the Shell): print(7 + 10).
  • Hit Submit Answer to execute the Python script and receive feedback.

Exercise

# Example, do not modify!
print(5 / 8)# Print the sum of 7 and 10
print(7+10)

Any comments?

Something that Hugo didn’t mention in his videos is that you can add comments to your Python scripts. Comments are important to make sure that you and others can understand what your code is about.

To add comments to your Python script, you can use the # tag. These comments are not run as Python code, so they will not influence your result. As an example, take the comment in the editor, # Division; it is completely ignored during execution.

Instructions

Above the print(7 + 10), add the comment

Exercise

# Division
print(5 / 8)# Addition
print(7 + 10)

Python as a calculator

Python is perfectly suited to do basic calculations. Apart from addition, subtraction, multiplication and division, there is also support for more advanced operations such as:

Exponentiation: . This operator raises the number to its left to the power of the number to its right. For example 42 will give 16.
Modulo: %. This operator returns the remainder of the division of the number to the left by the number on its right. For example 18 % 7 equals 4.
The code in the script gives some examples.

Instructions

Suppose you have $100, which you can invest with a 10% return each year. After one year, it’s dollars, and after two years it’s . Add code to calculate how much money you end up with after 7 years, and print the result.

Exercise

# Addition, subtraction
print(5 + 5)
print(5 - 5)# Multiplication, division, modulo, and exponentiation
print(3 * 5)
print(10 / 2)
print(18 % 7)
print(4 ** 2)# How much is your $100 worth after 7 years?
num =100
for i in range(7):num *= 1.1
print(num)print(100*1.1**7)

Variable Assignment

In Python, a variable allows you to refer to a value with a name. To create a variable use =, like this example:

x = 5
You can now use the name of this variable, x, instead of the actual value, 5.

Remember, = in Python means assignment, it doesn’t test equality!

Instructions

Create a variable savings with the value 100.
Check out this variable by typing print(savings) in the script.

Exercise

# Create a variable savings
savings=100# Print out savings
print(savings)

Calculations with variables

Remember how you calculated the money you ended up with after 7 years of investing $100? You did something like this:

100 * 1.1 ** 7
Instead of calculating with the actual values, you can use variables instead. The savings variable you’ve created in the previous exercise represents the $100 you started with. It’s up to you to create a new variable to represent 1.1 and then redo the calculations!

Instructions

Create a variable growth_multiplier, equal to 1.1.
Create a variable, result, equal to the amount of money you saved after 7 years.
Print out the value of result.

Exercise

# Create a variable savings
savings = 100# Create a variable growth_multiplier
growth_multiplier =1.1# Calculate result
result = savings * growth_multiplier ** 7# Print out result
print(result)

Other variable types

In the previous exercise, you worked with two Python data types:

  • int, or integer: a number without a fractional part. savings, with the value 100, is an example of an integer.
  • float, or floating point: a number that has both an integer and fractional part, separated by a point. growth_multiplier, with the value 1.1, is an example of a float.

Next to numerical data types, there are two other very common data types:

  • str, or string: a type to represent text. You can use single or double quotes to build a string.
  • bool, or boolean: a type to represent logical values. Can only be True or False (the capitalization is important!).

Instructions

Create a new string, desc, with the value “compound interest”.
Create a new boolean, profitable, with the value True

Exercise

# Create a variable desc
desc = 'compound interest'# Create a variable profitable
profitable =True

Operations with other types

Instructions

Calculate the product of savings and growth_multiplier. Store the result in year1.
What do you think the resulting type will be? Find out by printing out the type of year1.
Calculate the sum of desc and desc and store the result in a new variable doubledesc.
Print out doubledesc. Did you expect this?

Exercise

savings = 100
growth_multiplier = 1.1
desc = "compound interest"# Assign product of growth_multiplier and savings to year1
year1 = savings * growth_multiplier# Print the type of year1
print(type(year1))# Assign sum of desc and desc to doubledesc
doubledesc = desc + desc# Print out doubledesc
print(doubledesc)

Type conversion

Hugo mentioned that different types behave differently in Python.

When you sum two strings, for example, you’ll get different behavior than when you sum two integers or two booleans.

In the script some variables with different types have already been created. It’s up to you to use them.

Instructions

Hit Run Code to run the code. Try to understand the error message.
Fix the code such that the printout runs without errors; use the function str() to convert the variables to strings.
Convert the variable pi_string to a float and store this float as a new variable, pi_float.

Exercise

# 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)

DataCamp中Introduction to Python之Python Basics练习相关推荐

  1. DataCamp中Introduction to Python之NumPy

    这一节开始练习numpy Your First NumPy Array In this chapter, we're going to dive into the world of baseball. ...

  2. DataCamp中Introduction to Python之Python Lists练习

    这一节主要讲的是列表练习 Create a list As opposed to int, bool etc., a list is a compound data type; you can gro ...

  3. python 加载动图_在浏览器中使用TensorFlow.js和Python构建机器学习模型(附代码)...

    大数据文摘授权转载自数据派THU 作者:MOHD SANAD ZAKI RIZVI 本文主要介绍了: TensorFlow.js (deeplearn.js)使我们能够在浏览器中构建机器学习和深度学习 ...

  4. 独家 | 在浏览器中使用TensorFlow.js和Python构建机器学习模型(附代码)

    作者:MOHD SANAD ZAKI RIZVI 翻译:吴金笛 校对:丁楠雅 本文约5500字,建议阅读15分钟. 本文首先介绍了TensorFlow.js的重要性及其组件,并介绍使用其在浏览器中构建 ...

  5. SQL Server中数据透视表的Python脚本

    This article talks about Python scripts for creating pivot tables in multiple ways. 本文讨论了以多种方式创建数据透视 ...

  6. linux tensorflow demo_独家 | 在浏览器中使用TensorFlow.js和Python构建机器学习模型(附代码)...

    作者:MOHD SANAD ZAKI RIZVI 翻译:吴金笛 校对:丁楠雅 本文约5500字,建议阅读15分钟. 本文首先介绍了TensorFlow.js的重要性及其组件,并介绍使用其在浏览器中构建 ...

  7. 在翻译PEP8中学习 -- Style Guide for Python Code

    翻译了好久, 终于把这篇文档翻完了, 学到很多. 自从考研结束后就没有翻译过文章了, 一开始还以为考研英语78分的我翻译能力还可以, 结果打脸. 凡是得练习啊! 官方原文: PEP8 Style Gu ...

  8. python语言中一切皆对象_2 python 中一切皆对象

    python 中一皆对象 在 python 编程语言中所有的一切都是对象 , python 语言相对于 C++ 或 java 这样的语言 (静态语言), 面向对象这个思想更加的彻底. 2.1 函数和类 ...

  9. 【Python】Python中令人头疼的变量作用域问题,终于弄清楚了

    [Python]Python中令人头疼的变量作用域问题,终于弄清楚了_fengdu78的博客-CSDN博客 [Python]Python中令人头疼的变量作用域问题,终于弄清楚了_fengdu78的博客 ...

最新文章

  1. 中兴视讯服务器 广州,中兴高清视讯服务器ZXV10 M900
  2. 论坛报名 | 智能信息检索与挖掘的最新进展和挑战
  3. 【CyberSecurityLearning 48】PHP Cookie 和 SESSION
  4. Dojo实现Tabs页报错(二)
  5. Linux中的文件被异常删除的排查思路
  6. 后通用芯片时代: 专用芯片兴起背后的经济学
  7. 德力西双电源自动转换开关说明书_聊聊双电源转换的那点事。
  8. ajax的总结和使用
  9. rocketmq新扩容的broker没有tps_揭秘 RocketMQ 新特性以及在金融场景下的实践
  10. 算法 之 栈的简单讲解
  11. 博弈论(一)基本概念
  12. Flash builder 4.6下载以及安装
  13. 条码扫描枪在仓库管理无线网络AP解决方案
  14. 智能功放 ACS 保护
  15. 饮用水公司配送管理系统可行性报告
  16. Horner method
  17. 1449 后缀表达式
  18. 人工智障也刷题!Kaggle 入门之实战泰坦尼克号
  19. 容器服务(四): Kubernetes 搭建与核心原理
  20. Unity3D学习之添加耀斑,添加天空,添加画中画。

热门文章

  1. 北京气象学院计算机,何登科-地球科学与测绘工程学院-中国矿业大学(北京)...
  2. React框架课时二认识项目的结构目录一
  3. android带边框的RelativeLayout
  4. ARP劫持攻击(实验寒假补)
  5. java开发工具有哪些?
  6. 容量法和库仑法的异同点_容量、库仑、点位滴定法的比较
  7. vivo手机助手强势来袭!
  8. XSS学习笔记:XSS Game(xss.pwnfunction.com)1-11通关全解
  9. 可以检测手机帧率和温度的软件_没有特异功能 手机软件是如何测得电池温度的...
  10. 异步API中promise解决回调地狱问题和异步函数async终极解决方案