Variable Sorts
Variable
are only held memory areas to store values or in other words an address to the Value. This
implies when you make a variable you reserve some
space in memory.
In
view of the information kind of a variable, the interpreter allots memory and
chooses what can be put away in the held memory. Along these lines, by
allotting different data types to
variables, you can store whole numbers, decimals or characters in
these Variable.
The
most effective method to Assign
Values to Variable in Python?
Python
Variable needs explicit declaration
to save memory space. The presentation happens consequently when you assign (give)
a value to a variable. The equivalent sign (=) is utilized to allot values
to Variable.
The
operand to left side of the = operator
is the name of the variable and the operand to right side of the = operator is the value stored in the
variable.
For
instance –
#!
/usr/bin/python
Counter
= 10 # A number task
Miles
= 100.0
# A skimming point
Name
= "Muong" # A string
print
counter
print
miles
print
name
Here,
10, 100.0 and "Muong" are the values assigned
to counter, miles, and name Variable, separately. This creates the following outcome −
10
100.0
Muong
Multiple Assignment
Python
permits you to appoint a single
value to more than one Variable at a same time. For instance −
a
= b = c = 5
Here,
a number variable is made with the value 5, and every one of the three
Variable are allocated to a same memory area. You can likewise assign multiple objects to
different Variable. For instance −
a,
b, c = 1, 2, "Muong"
Here,
two whole number articles with qualities 1 and 2 are appointed to Variable a and
b separately, and one string object with the value "Muong" is
allocated to the variable c.
Standard Data Types
The
information put away in memory can be of many sorts. For instance, a man's age
is put away as a numeric value and his or her address is put away as
alphanumeric characters. Python has different standard data types that are utilized to characterize
the operations conceivable on them and the storage method for each of them.
Python
has five standard information sorts −
·
Numbers
·
String
·
Rundown
·
Tuple
·
Dictionary
Python Numbers
Number
data types store numeric values. Number items are
made when you assign a value to them. For
instance −
var1
= 1
var2
= 10
You
can likewise erase the reference to a number question by utilizing the del
proclamation. The punctuation of the del proclamation is −
del
var1[,var2[,var3[....,varN]]]]
You
can erase a single object or multiple objects by utilizing the del statement. For instance −
del
var
del
var_a, var_b
Python
supports four distinctive numerical
sorts −
·
int
(marked whole numbers)
·
long
(whole numbers, they can likewise be represented to in octal and hexadecimal)
·
float (float point real values)
·
complex
(complex numbers)
Illustrations
Here
are a few cases of numbers −
int
|
long
|
float
|
complex
|
10
|
51924361L
|
0
|
3.14j
|
100
|
-0x19323L
|
15.2
|
45.j
|
-786
|
0122L
|
-21.9
|
9.322e-36j
|
80
|
0xDEFABCECBDAECBFBAEl
|
32.3+e18
|
.876j
|
-490
|
535633629843L
|
-90
|
-.6545+0J
|
-0x260
|
-052318172735L
|
-3.25E+101
|
3e+2J
|
0x69
|
-4721885298529L
|
70.2-E12
|
4.53e-7j
|
·
Python
permits you to utilize a lowercase l with long, however it is recommended that you utilize just a
capitalized L to stay away from confusion with the number 1.
Python shows long numbers with a capitalized L.
·
A
complex number comprises 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.
Python Strings
Strings
in Python are recognized as a contiguous
set of characters represented in the quotation marks. Python
takes into account either combines of single or twofold quotes. Subsets of
strings can be taken utilizing the slice administrator ([ ] and
[:]) with indexes beginning at 0 in the
start of the string and working their way from - 1 toward the end.
The
plus (+) sign
is the string concatenation operator and the asterisk (*) is repetition operator.
For
instance −
#!/usr/bin/python
str
= 'Hi World!'
print
str # Prints finish string
print
str[0] # Prints first character of the string
print
str[2:5] # Prints characters beginning from third to fifth
print
str[2:] # Prints string beginning from third character
print
str * 2 # Prints string two circumstances
print
str + "TEST" # Prints linked string
This
will deliver the accompanying outcome −
Hi
World!
H
llo
llo
World!
Hi
World!Hello World!
Hi
World!TEST
Python Lists
Lists are the most flexible of Python's compound
data types. A List
contains things separated by commas and encased
inside square sections ([]). To some degree, list are like arrays in
C. One difference between them is that the items belonging to a list can be of
different data type.
The
values put away in a list can
be accessed by utilizing the slice operator ([ ] and [:]) with lists
beginning at 0 in the start of the list and working their approach to end - 1.
The plus (+) sign is the list concatenation operator,
and the asterisk (*) is the repetition operator.
For
instance −
#!/usr/bin/python
list
= [ 'abcd', 786 , 2.23, 'john', 70.2 ]
tinylist
= [123, 'john']
print
list # Prints finish list
print
list[0] # Prints first component of the rundown
print
list[1:3] # Prints components beginning from second till third
print
list[2:] # Prints components beginning from third component
print
tinylist * 2 # Prints list two circumstances
print
list + tinylist # Prints linked records
This
create the accompanying outcome −
['abcd',
786, 2.23, 'john', 70.200000000000003]
abcd
[786,
2.23]
[2.23,
'john', 70.200000000000003]
[123,
'john', 123, 'john']
['abcd',
786, 2.23, 'john', 70.200000000000003, 123, 'john']
Python Tuples
A
tuple is another sequence data type that is
like the list. A tuple comprises of various qualities separated by
commas. Unlike to list, tuples are encased within parentheses.
The
fundamental contrasts amongst lists
and tuples are: Lists are encased in sections ([
]) and their components and size can be changed, while tuples are encased
in enclosures ( ) and can't be redesigned. Tuples can be considered as read-only lists. For instance −
#!/usr/bin/python
tuple
= ( 'abcd', 786 , 2.23, 'john', 70.2 )
tinytuple
= (123, 'john')
print
tuple # Prints finish list
print
tuple[0] # Prints first component of the rundown
print
tuple[1:3] # Prints components beginning from second till third
print
tuple[2:] # Prints components beginning from third component
print
tinytuple * 2 # Prints list two circumstances
print
tuple + tinytuple # Prints linked records
This
create the accompanying outcome −
('abcd',
786, 2.23, 'john', 70.200000000000003)
abcd
(786,
2.23)
(2.23,
'john', 70.200000000000003)
(123,
'john', 123, 'john')
('abcd',
786, 2.23, 'john', 70.200000000000003, 123, 'john')
The
accompanying code is invalid with tuple, on the grounds that we attempted to redesign or update a
tuple, which is not permitted. Same case is possible with
list −
#!/usr/bin/python
tuple
= ( 'abcd', 786 , 2.23, 'john', 70.2 )
list
= [ 'abcd', 786 , 2.23, 'john', 70.2 ]
tuple[2]
= 1000 # Invalid language structure with tuple
list[2]
= 1000 # Substantial sentence structure with rundown
Python Dictionary
Python's
dictionaries are somewhat hash table
sort. They work like associative
arrays or hashes found in Perl and comprise of key-value sets. A Dictionary
key can be any Python sort, yet are normally numbers or strings. Values, then
again, can be arbitrary Python
object.
Dictionaries are encased by curly braces
({ }) and qualities can be allocated and gotten to utilizing square
supports ([ ]). For instance −
#!/usr/bin/python
dict
= {}
dict['one']
= "This is one"
dict[2]
= "This is two"
tinydict
= {'name': 'john','code':6734, 'dept': 'sales'}
print
dict['one'] # Prints value for "one" key
print
dict[2] # Prints value for 2 key
print
tinydict # Prints finish Dictionary
print
tinydict.keys() # Prints all the keys
print
tinydict.values() # Prints every one of the qualities
This
create the accompanying outcome −
This
is one
This
is two
{'dept':
'deals', 'code': 6734, 'name': 'john'}
['dept',
'code', 'name']
['sales',
6734, 'john']
Dictionaries have no concept of order among elements.
It is wrong to state that the components are "out of order "; they are
basically unordered.
Data Type Conversion
In
some cases, you may need to perform changes between the built-in types. To change
over between types,
you basically utilize the type
name as a function.
There
are a several built-in functions
to perform transformation starting with one data type then onto the next. These functions return a new object
representing the converted value.
Function
|
Description
|
int(x [,base])
|
Converts x to an
integer. base specifies the base if x is a string.
|
long(x [,base] )
|
Converts x to a long
integer. base specifies the base if x is a string.
|
float(x)
|
Converts x to a
floating-point number.
|
complex(real
[,imag])
|
Creates a complex
number.
|
str(x)
|
Converts object x to
a string representation.
|
repr(x)
|
Converts object x to
an expression string.
|
eval(str)
|
Evaluates a string
and returns an object.
|
tuple(s)
|
Converts s to a
tuple.
|
list(s)
|
Converts s to a
list.
|
set(s)
|
Converts s to a set.
|
dict(d)
|
Creates a
dictionary. d must be a sequence of (key, value) tuples.
|
frozenset(s)
|
Converts s to a
frozen set.
|
chr(x)
|
Converts an integer
to a character.
|
unichr(x)
|
Converts an integer
to a Unicode character.
|
ord(x)
|
Converts a single
character to its integer value.
|
hex(x)
|
Converts an integer
to a hexadecimal string.
|
oct(x)
|
Converts an integer
to an octal string.
|
0 comments:
Post a Comment