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

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.

主站蜘蛛池模板: 尉犁县| 昌乐县| 咸丰县| 策勒县| 儋州市| 玉环县| 星座| 宁强县| 永定县| 石景山区| 盐源县| 集安市| 鄄城县| 彭阳县| 淳安县| 兴义市| 吐鲁番市| 隆尧县| 渭南市| 河北区| 延川县| 雷波县| 张家川| 林周县| 靖远县| 石泉县| 吴忠市| 原阳县| 肥乡县| 武夷山市| 额尔古纳市| 三明市| 湘西| 桐庐县| 嘉峪关市| 溧阳市| 侯马市| 凤台县| 正阳县| 西林县| 乌拉特中旗|