官术网_书友最值得收藏!

Numbers

Numbers, as the name suggests, covers all the numeric data types, including both integer and floating data types. Earlier in this chapter, we saw that to use an integer or a float, we can simply declare the variable and assign an integer or a float value. Now, let's write a proper Python script and explore how to use numbers. Name the script numbers.py which is shown as follows:

The preceding screenshot show a simple Python script that adds an integer with a float and then prints the sum. To run the script, we can type the python3 numbers.py command, as follows:

You might have noticed that the command at the beginning of the script says #! /usr/bin/python. What this line does is make your code executable. After the privileges of the script have changed and it has been made executable, the command says that if an attempt is made to execute this script, then we should go ahead and execute it with python3, which is placed in the /usr/bin/python3 path. This can be seen in the following example:

If we observe the print command, we can see that the string formatter is %s. To fill it in with the actual value, the second argument to the print function is passed:

To convert a string into its equivalent integer or float value, we can use the built-in int() and float() functions.

主站蜘蛛池模板: 武宣县| 嘉荫县| 渝北区| 武功县| 松溪县| 惠州市| 志丹县| 潼关县| 昌宁县| 精河县| 兴化市| 绥阳县| 咸宁市| 旬阳县| 读书| 祁东县| 吉隆县| 营口市| 清丰县| 公安县| 炉霍县| 富民县| 松江区| 天长市| 东海县| 巴林右旗| 明光市| 丰城市| 汪清县| 曲周县| 米泉市| 涟源市| 东辽县| 青神县| 鹤山市| 乳山市| 浑源县| 卫辉市| 林西县| 噶尔县| 朝阳市|