Python Numeric Types and Casting Explained
In Python, there are three main numeric types - int, float, and complex. This tutorial covers how to work with each type, create variables, and verify object types using the type() function. Additionally, it explains casting in Python using constructor functions like int(), float(), and str(). Examples are provided to illustrate the processes of constructing different numeric types and converting between them.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Python Numbers here are three numeric types in Python: int , float , complex Variables of numeric types are created when you assign a value to them: To verify the type of any object in Python, use the type() function: 2
Python Numbers Int Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length. Example x = 1 y = 35656222554887711 z = -3255522 print(type(x)) print(type(y)) print(type(z)) 3
Python Numbers Float Float, or "floating point number" is a number, positive or negative, containing one or more decimals. Example x = 1.10 y = 1.0 z = -35.59 print(type(x)) print(type(y)) print(type(z)) 4
Python Numbers Complex Complex numbers are written with a "j" as the imaginary part: Example x = 3+5j y = 5j z = -5j print(type(x)) print(type(y)) print(type(z)) 5
Python Numbers Casting in python is therefore done using constructor functions: int() - constructs an integer number from an integer literal, a float literal (by rounding down to the previous whole number), or a string literal (providing the string represents a whole number) float() - constructs a float number from an integer literal, a float literal or a string literal (providing the string represents a float or an integer) str() - constructs a string from a wide variety of data types, including strings, integer literals and float literals Example No.1 Integers: x = int(1) y = int(2.8) # y will be 2 z = int("3") # z will be 3 # x will be 1 6
Python Numbers Example No.2 Floats: x = float(1) y = float(2.8) z = float("3") w = float("4.2") # w will be 4.2 # x will be 1.0 # y will be 2.8 # z will be 3.0 Example No.3 Strings: x = str("s1") # x will be 's1' y = str(2) # y will be '2' z = str(3.0) # z will be '3.0' 7
print print: Produces text output on the console. Syntax: print("Message ) print(Expression) Prints the given text message or expression value on the console, and moves the cursor down to the next line. print (Item1, Item2, ..., ItemN) Prints several messages and/or expressions on the same line. Examples: print("Hello, world! ) age = 45 print("You have", 65 - age, "years until retirement ) Output: Hello, world! You have 20 years until retirement 8
input input : Reads a number from user input. You can assign (store) the result of input into a variable. Example: age = input("How old are you? ") print("Your age is", age) age=int(age) print("You have", 65 - age, "years until retirement ) Output: How old are you? 53 Your age is 53 You have 12 years until retirement 9