Python- variable+Numbers
Good Morning boys.
Today is the practical class. I hope your practice sessions for Python are continuing at home. Keep the good work going. Today is the day, don't wait.
_____________________________________________________________________________
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
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:
intfloatcomplex
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,
a=10.0
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 ma'am
ReplyDeleteBrian
Good morning mam
ReplyDeleteWe have our class at 10:40am. How are u missing your other class??? I am testing something here. Pls join your other subject. above attendance will not be marked
ReplyDeleteGood morning maam
DeletePlease mark your attendance now
ReplyDeleteGeeta Sehgal is inviting you to a scheduled Zoom meeting.
ReplyDeleteTopic: Geeta Sehgal's Zoom Meeting
Time: Apr 6, 2020 11:00 PM India
Join Zoom Meeting
https://us04web.zoom.us/j/281195094?pwd=NWtHSkZmY241TFI2NFlOM1hXa3Rmdz09
Meeting ID: 281 195 094
Password: 006946
Class at 11 am today
Good Morning Maam
ReplyDeletePls read the content carefully. Then we will move to zoom class at 11am sharp. Link has been shared in the comment section
ReplyDeletein next two minutes, pls join the zoom class
ReplyDeleteThere is some error on zoom
ReplyDeleteNew invite:
Geeta Sehgal is inviting you to a scheduled Zoom meeting.
Topic: Geeta Sehgal's Zoom Meeting
Time: Apr 7, 2020 11:15 AM India
Join Zoom Meeting
https://us04web.zoom.us/j/114383799?pwd=bkhPcTVERkhYdlJFYnFTbWhidVRvdz09
Meeting ID: 114 383 799
Password: 000894
Are you clear with the sharing done today?
ReplyDeletePls share doubts, if any
GOOD MORNING MA'AM
ReplyDelete-ARTHAM
Good morning ma'am
ReplyDelete-Geet
Mam, I missed the zoom class, since we had science going on till 11:20.
ReplyDeleteIs it possible to have recording of to that class. Or notes, if available.
Geet.
Ma'am I am back from zoom
ReplyDeleteTo blogs
There seems to be some problem. Lets try google meet
ReplyDeleteJoin Hangouts Meet
ReplyDeletemeet.google.com/xrq-gcow-ffa
click on the above link and join
Sorry
ReplyDeleteMeeting ID
meet.google.com/xrq-gcow-ffa
Ma'am i'm not able to join
ReplyDelete-Geet
Ma'am I am not able to join
ReplyDelete