Python Variables, Data Types
A variable is a location in memory used to store some data.
Variables carry unique names to differentiate between different memory location.
In python variable declaration is not required.
Just assigning value to variable will create the variable with the type of data value assigned, this is taken care internally.
Assignment
a = 10 #variable a will be integer
b = 23.58 #variable b will be float
c = "python" #variable c will be string
>>>type(a)
<class 'int'>
Multiple assignment
a, b, c = 10, 23.58, "python"
a = b = c = 100 #assigns 100 to all variables
a = 45, "bgt", 34 #create a tuple variable
Data Types
Every value in Python has a data type.
In Python everything is an object, data types are actually classes, and variables are instance (object) of these classes.
Numerical Data Types - int, long, float, complex
A floating point number is accurate up to 15 decimal places.
Strings
Strings in Python are identified as a continuous set of characters in between quotation marks (either single quote or double quote).
slice operator [] and [:]
index starting at 0
at the end index -1
+ used for concatenation of string
* used for repetition of string
>>>str1 = 'python'
>>>str1[0]
'p'
>>>str1[-1]
'n'
>>>str1[2:5]
'tho'
>>>'*' * 5
'*****'
Comments
Post a Comment