python 算術運算

1. 算術運算符與優先級

# -*- coding:utf-8 -*-# 運算符含有+,-,*,/,**,//,%
# ** 表示^ , 也就是次方
a = 2 ** 4
print '2 ** 4 = ', aa = 16 / 5
print '16 / 5 = ', aa = 16.0 / 5
print '16.0 / 5 = ', a# 結果再進行一次floor
a = 16.0 // 5.0
print '16.0 // 5.0 = ', aa = 16 // 5
print '16 // 5 = ', a
# 在Python 2.7中,’/’執行傳統除法,如果操作數都是整數的話,執行截斷的整數除法(即對于結果只保留整數部分),否則,執行浮點除法(保留余數);’//’執行Floor除法,與Python3.0一樣,對于整數執行截斷除法,浮點數執行浮點除法。
# 在Python 3.0中,’/’總是執行真除法,不管操作數的類型,都會返回包含任何余數的浮點結果;’//’執行Floor除法,截除掉余數并且針對整數操作數返回一個整數,如果有任何一個操作數是浮點數,則返回一個浮點數。
a = -16 // 5
print '-16 // 5 = ', a
# 往下取整, 返回不大于x的整數

結果:
2 ** 4 =  16
16 / 5 =  3
16.0 / 5 =  3.2
16.0 // 5.0 =  3.0
16 // 5 =  3
-16 // 5 =  -4# 運算符的優先級
# + - 小于 * /
a = 5 * 6 // 4
print '5 * 6 // 4 = ', a # * 等同于 //
a = 5 * 2 ** 2
print '5 * 2 ** 2 = ', a # ** 優于 *
a = 5 * 3 % 2
print '5 * 3 % 2 = ', a # * 等同于 %
# 所以加括號吧

結果:
5 * 6 // 4 =  7
5 * 2 ** 2 =  20
5 * 3 % 2 =  1

?整數運算

python中整數運算很神奇,不限制長度,也就是沒有溢出這種說法。不過在64位能表示的情況下,數字的類型為int,超過2^63次方之后就變成了long類型。

而且目前所了解到的,整數的各類運算速度都還挺快,都是經過優化的。

浮點數運算

python中的浮點數運算就可以等于double的運算了。

?

轉載于:https://www.cnblogs.com/chenhuan001/p/8011672.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390584.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390584.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390584.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

c語言編程時碰到取整去不了_碰到編程墻時如何解開

c語言編程時碰到取整去不了Getting stuck is part of being a programmer, no matter the level. The so-called “easy” problem is actually pretty hard. You’re not exactly sure how to move forward. What you thought would work doesn’t.無論身在何處,陷…

初創公司怎么做銷售數據分析_為什么您的初創企業需要數據科學來解決這一危機...

初創公司怎么做銷售數據分析The spread of coronavirus is delivering a massive blow to the global economy. The lockdown and work from home restrictions have forced thousands of startups to halt expansion plans, cancel services, and announce layoffs.冠狀病毒的…

leetcode 909. 蛇梯棋

題目 N x N 的棋盤 board 上,按從 1 到 N*N 的數字給方格編號,編號 從左下角開始,每一行交替方向。 例如,一塊 6 x 6 大小的棋盤,編號如下: r 行 c 列的棋盤,按前述方法編號,棋盤格…

Python基礎之window常見操作

一、window的常見操作: cd c:\ #進入C盤d: #從C盤切換到D盤 cd python #進入目錄cd .. #往上走一層目錄dir #查看目錄文件列表cd ../.. #往上上走一層目錄 二、常見的文件后綴名: .txt 記事本文本文件.doc word文件.xls excel文件.ppt PPT文件.exe 可執行…

WPF效果(GIS三維篇)

二維的GIS已經被我玩爛了,緊接著就是三維了,哈哈!先來看看最簡單的效果: 轉載于:https://www.cnblogs.com/OhMonkey/p/8954626.html

css注釋_CSS注釋示例–如何注釋CSS

css注釋Comments are used in CSS to explain a block of code or to make temporary changes during development. The commented code doesn’t execute.CSS中使用注釋來解釋代碼塊或在開發過程中進行臨時更改。 注釋的代碼不執行。 Both single and multi-line comments in…

r軟件時間序列分析論文_高度比較的時間序列分析-一篇論文評論

r軟件時間序列分析論文數據科學 , 機器學習 (Data Science, Machine Learning) In machine learning with time series, using features extracted from series is more powerful than simply treating a time series in a tabular form, with each date/timestamp …

leetcode 168. Excel表列名稱

題目 給你一個整數 columnNumber ,返回它在 Excel 表中相對應的列名稱。 例如: A -> 1 B -> 2 C -> 3 … Z -> 26 AA -> 27 AB -> 28 … 示例 1: 輸入:columnNumber 1 輸出:“A” 示例 2&…

飛機訂票系統

1 #include <stdio.h>2 #include <stdlib.h>3 #include <string.h>4 #include <conio.h>5 typedef struct flightnode{6 char flight_num[10]; //航班號7 char start_time[10]; //起飛時間8 char end_time[10]; //抵達時間9 char st…

解決Mac10.13 Pod報錯 -bash: /usr/local/bin/pod: /System/Library/Frameworks/Ruby.fram

升級10.13以后Pod命令失效&#xff0c;解決辦法如下&#xff1a; 終端執行 brew link --overwrite cocoapods 復制代碼嘗試 Pod 命令是否已經恢復 若報錯繼續執行 brew reinstall cocoapodsbrew install rubybrew link --overwrite cocoapods 復制代碼嘗試 Pod 命令是否已經恢復…

angular示例_用示例解釋Angular動畫

angular示例為什么要使用動畫&#xff1f; (Why use Animations?) Modern web components frequently use animations. Cascading Style-sheets (CSS) arms developers with the tools to create impressive animations. Property transitions, uniquely named animations, mu…

selenium抓取_使用Selenium的網絡抓取電子商務網站

selenium抓取In this article we will go through a web scraping process of an E-Commerce website. I have designed this particular post to be beginner friendly. So, if you have no prior knowledge about web scraping or Selenium you can still follow along.在本文…

劍指 Offer 37. 序列化二叉樹

題目 序列化是將一個數據結構或者對象轉換為連續的比特位的操作&#xff0c;進而可以將轉換后的數據存儲在一個文件或者內存中&#xff0c;同時也可以通過網絡傳輸到另一個計算機環境&#xff0c;采取相反方式重構得到原數據。 請設計一個算法來實現二叉樹的序列化與反序列化…

ie8 ajaxSubmit 上傳文件提示下載

轉載 解決ie下ajaxsubmit上傳文件提示下載文件問題 主要是應為放回類型為json&#xff0c;返回text/html轉載于:https://www.cnblogs.com/yang-C-J/p/8963278.html

一個簡單的 js 時間對象創建

JS中獲取時間很常見&#xff0c;湊湊熱鬧&#xff0c;也獲取一個時間對象試試 首先&#xff0c;先了解js的獲取時間函數如下&#xff1a; var myDate new Date(); //創建一個時間對象 myDate.getYear(); // 獲取當前年份&#xff08;2位&#x…

裁判打分_內在的裁判偏見

裁判打分News flash: being an umpire is hard. Their job is to judge whether a ball that’s capable of moving upwards of 100 MPH or breaking 25 inches crossed through an imaginary zone before being caught. I don’t think many would argue that they have it ea…

數據庫sql課程設計_SQL和數據庫-初學者完整課程

數據庫sql課程設計In this course, Mike Dane will teach you database management basics and SQL.在本課程中&#xff0c;Mike Dane將教您數據庫管理基礎知識和SQL。 The course starts off with Mike helping you install MySQL on Windows or Mac. Then he explores topic…

LCP 07. 傳遞信息

小朋友 A 在和 ta 的小伙伴們玩傳信息游戲&#xff0c;游戲規則如下&#xff1a; 有 n 名玩家&#xff0c;所有玩家編號分別為 0 &#xff5e; n-1&#xff0c;其中小朋友 A 的編號為 0 每個玩家都有固定的若干個可傳信息的其他玩家&#xff08;也可能沒有&#xff09;。傳信息…

微信公眾號自動回復加超鏈接最新可用實現方案

你在管理微信號時是否會有自動回復或者在關鍵字觸發自動回復加一個超鏈接的需求呢&#xff1f;例如下圖像王者榮耀這樣&#xff1a; 很多有開發經驗的朋友都知道微信管理平臺會類似富文本編輯器&#xff0c;第一想到的解決方案會是在編輯框中加<a href網址 >顯示文字<…

devops開發模式流程圖_2020 Web開發人員路線圖–成為前端,后端或DevOps開發人員的視覺指南

devops開發模式流程圖There are many ways you can go about picking up the skills you need to become a developer.您可以采用多種方法掌握成為開發人員所需的技能。 There are linear curriculums that teach you a bit of everything - like freeCodeCamps full stack de…