Python- Numeric literals
Good Morning boys.
I hope your practice sessions for Python are continuing at home. Keep the good work going. Today is the day, don't wait.
------------------------------------------------------------------------
The Google Meet Class link for today's class is:
_____________________________________________________________________________
By the end of this session, you will be able understand and apply the following concepts:
Answers for the Exercise questions based on Strings given in the previous session are as follows.
PLEASE CHECK YOUR ANSWERS
I hope your practice sessions for Python are continuing at home. Keep the good work going. Today is the day, don't wait.
------------------------------------------------------------------------
The Google Meet Class link for today's class is:
By the end of this session, you will be able understand and apply the following concepts:
- What are variable?
- Ways of assigning variables
- Concepts of multiple assignments
- Introduction to data types in Python (Just introduction)
- Numeric data type - types and exact difference
Answers for the Exercise questions based on Strings given in the previous session are as follows.
PLEASE CHECK YOUR ANSWERS
- Is there a difference between "Geeta" and 'Geeta'? Justify.
No difference as a string can be either enclosed in a single quotesas in 'Geeta' or double quotes as in "Geeta"2. Suppose
- Predict the output of the following:
>>> print("SCS"*5) SCSSCSSCSSCSSCS >>> print(("SCS"," ")*5) ('SCS', ' ', 'SCS', ' ', 'SCS', ' ', 'SCS', ' ', 'SCS', ' ') >>> print("I"+"Love"+"Columbas") ILoveColumbas >>> print("I","Love","Columbas") I Love Columbas
str='I LOVE COLUMBAS'
Predict the output of the following:
print str # Prints complete string
Output: I LOVE COLUMBAS
print str * 2 # Prints string two times
Output:I LOVE COLUMBASI LOVE COLUMBAS
print str + " MUSIC"
I LOVE COLUMBAS Music
------------------------------------------------------------------------------------
PYTHON VARIABLES
Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.
Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals or characters in these variables.
Assigning Values to Variables
Python variables do not need explicit declaration to reserve memory space. The declaration happens automatically when you assign a value to a variable. The equal sign (=) is used to assign values to variables.
The operand to the left of the = operator is the name of the variable and the operand to the right of the = operator is the value stored in the variable. For example −
(LIVE DEMO- Hyperlink to help you understand the working practically)
#!/usr/bin/python counter = 100 # An integer assignment miles = 1000.0 # A floating point name = "John" # A string print counter print miles print name
Here, 100, 1000.0 and "John" are the values assigned to counter, miles, and name variables, respectively. This produces the following result −
100 1000.0 John
Multiple Assignment
Python allows you to assign a single value to several variables simultaneously. For example −
a = b = c = 1
Here, an integer object is created with the value 1, and all three variables are assigned to the same memory location. You can also assign multiple objects to multiple variables. For example −
a,b,c = 1,2,"john"
Here, two integer objects with values 1 and 2 are assigned to variables a and b respectively, and one string object with the value "john" is assigned to the variable c.
SO, effectively the above SINGLE statement is assigning three variable a,b,c to the following values :
a=1
b=2
c="john"
SO, effectively the above SINGLE statement is assigning three variable a,b,c to the following values :
a=1
b=2
c="john"
Standard Data Types
The data stored in memory can be of many types. For example, a person's age is stored as a numeric value and his or her address is stored as alphanumeric characters. Python has various standard data types that are used to define the operations possible on them and the storage method for each of them.
Python has five standard data types −
- Numbers
- String
- List
- Tuple
- Dictionary
___________________________________________________
Python Numbers
There are three numeric types in Python:
int
float
complex
Variables of numeric types are created when you assign a value to them:
Example
x = 1 # int
y = 2.8 # float
z = 1j # complex
To verify the type of any object in Python, use the
type()
function:Example
print(type(x))
print(type(y))
print(type(z))
Int
Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length.
Example
Integers:
x = 1
y = 35656222554887711
z = -3255522
print(type(x))
print(type(y))
print(type(z))
Float
Float, or "floating point number" is a number, positive or negative, containing one or more decimals.
Example
Floats:
x = 1.10
y = 1.0
z = -35.59
print(type(x))
print(type(y))
print(type(z))
Float can also be scientific numbers with an "e" to indicate the power of 10.
Example
Floats:
x = 35e3
y = 12E4
z = -87.7e100
print(type(x))
print(type(y))
print(type(z))
Complex
Complex numbers are written with a "j" as the imaginary part:
Example
Complex:
x = 3+5j
y = 5j
z = -5j
print(type(x))
print(type(y))
print(type(z))
Type Conversion
You can convert from one type to another with the
int()
, float()
, and complex()
methods. This is also known as TYPECASTING.Example
Convert from one type to another:
x = 1 # int
y = 2.8 # float
z = 1j # complex
#convert from int to float:a = float(x)
#convert from float to int:b = int(y)
#convert from int to complex:c = complex(x)
print(a)
print(b)
print(c)
print(type(a))
print(type(b))
print(type(c))
Examples
Here are some examples of numbers −
int | long | float | complex |
---|---|---|---|
10 | 51924361L | 0.0 | 3.14j |
100 | -0x19323L | 15.20 | 45.j |
-786 | 0122L | -21.9 | 9.322e-36j |
080 | 0xDEFABCECBDAECBFBAEl | 32.3+e18 | .876j |
-0490 | 535633629843L | -90. | -.6545+0J |
-0x260 | -052318172735L | -32.54e100 | 3e+26J |
0x69 | -4721885298529L | 70.2-E12 | 4.53e-7j |
- Python allows you to use a lowercase l with long, but it is recommended that you use only an uppercase L to avoid confusion with the number 1. Python displays long integers with an uppercase L.
- A complex number consists of an ordered pair of real floating-point numbers denoted by x + yj, where x and y are the real numbers and j is the imaginary unit.
----------------------------------------------------------------------------------------------------------------
EXERCISE
- What are variable? Why do we need variables?
- What can you say about declaration of variable in Python? Give example.
- In the following statements,
b=57
c="a"
d="SCS"
a) what is the data type of the variables a,b,c and d ?
b) what will be the output of the statements that follow:
print(a)
print(b)
print(c)
print(d)
4. What is the purpose of the multiple assignment statements?
5. What are the numeric data types available? Give example
6. What is Typecasting? Name the functions used for typecasting.
7. Give the output of the following set of statements
a= 27.325
b=int(a)
print("b=",b)
Good Morning. Pls share your name and roll no and join the Google meet class. link has been given at the top
ReplyDeleteGood Morning Ma'am
ReplyDeleteKavyansh Bagotra 9D
Roll No. 6
Good morning ma’am!! Arjav Jain of class 9D here
ReplyDeleteGood morning ma'am
ReplyDeleteMy name is soumil arora
Good morning mam I am Gunamay prasad of class 9 D
ReplyDeleteGood morning ma'am I am vansh bharti of class 9D
ReplyDeleteGood morning ma’am
ReplyDeleteMy name is Ryan Rahuel Valentine from class 9-D
Good morning mam I am Arush Jain of class 9th D
ReplyDeleteGood morning ma'am this side piyushdawla class9D
ReplyDeleteGood Morning Ma'am I am Hrishit Deb of class 9-D
ReplyDeleteGood morning ma'am this is Riyan Gabriel from class 9-D
ReplyDeleteGood morning mam I am Sandarbh of class 9-D
ReplyDeleteGood morning mam I am Kritik Kuraria
ReplyDeleteGood morning mami am kenneth barrett of class 9-D
ReplyDeleteGood morning ma’am I am Aditya Narayan Padhy of class 9D
ReplyDeleteGood morning ma'am this is Vincent Zacharias
ReplyDeleteGood morning ma'am.
ReplyDeleteMy name is Ojas Khanna
Good afternoon ma'am! Ma'am don't we have to follow the syntax print(str) in python as it is mentioned that print str which does not have parentheses
ReplyDeleteRegards,
Arjav Jain
Seeing the blogs after a long time
ReplyDelete