1 / 16

Understanding Numbers in Python: Types, Operations, and Dynamic Typing

This chapter delves into the essential numerical types in Python, focusing on integers, floating-point numbers, and long integers. You will learn how to use various numeric operations, such as addition, subtraction, multiplication, and division, with practical examples that illustrate their functionality. The concept of dynamic typing in Python is also introduced, showing how variables can reference different objects over time. Additionally, we explore some mathematical tools available in Python, which enhance numerical processing capabilities.

oriel
Download Presentation

Understanding Numbers in Python: Types, Operations, and Dynamic Typing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 4 Numbers Bernard Chen 2007

  2. Python Build-in types • Numbers 3.1415 • Strings “Hello World” • Lists [1,2,3,4] • Dictionaries {‘test’: ‘yum’} • Files input=open(‘file.txt’, ‘r’)

  3. Numbers Many different types, we only provide 3 here for now: • Integer • Floating-point • Long integer

  4. Number in Action • Perhaps the best way to understand numerical objects is to see them in action. >>> a=3 #Name Created >>> b=4

  5. Number in Action >>> a+1, a-1 # (3+1), (3-1) (4,2) >>> b*3, b/2 (12,2)

  6. Number in Action >>> a%2, b**2 #remainder, power (1,16) >>> 2+4.0, 2.0**b #mix type (6.0,16.0) >>> c*2 #error

  7. Number in Action >>> b / 2 + a >>> b/(2.0+a) >>> b/(2+a)

  8. Number in Action >>> b / (2.0 + a) #auto echo output 0.80000000000004 >>> print b/(2.0+a) #print rounds off 0.8 digits

  9. Number in Action >>> 1 / 2.0 0.5 >>> 1/2 0

  10. Long Integers >>> 2L ** 200 >>> 2 ** 200

  11. Other Numeric Tools >>> import math >>> math.pi >>> math.e >>> int(2.567), round(2.567), round(2.567,2)

  12. Other Numeric Tools >>> abs(-42), 2**4, pow(2,4) (42, 16, 16) >>>int(2.567), round(2.567), round(2.4) (2, 3.0, 2.0) >>> round(2.567, 2) 2.569999999999998

  13. Dynamic Typing >>> a = 3 • Create an object to represent the value of 3 • Create the variable a, if it does not yet exist • Link the variable a to the new object 3

  14. a = 3

  15. Share Reference >>> a=3 >>> b=a

  16. Share Reference >>>a=3 >>>b=a >>>a=5

More Related