Python Variables & Datatype
Variables
In Python, we don't need to specify the type of variable because Python is a infer language and smart enough to get variable type.
Variable names can be a group of both the letters and digits, but they have to begin with a letter or an underscore.
It is recommended to use lowercase letters for the variable name. Rahul and rahul both are two different variables.
Datatype
Numeric
int
, float
and complex
class in Python.- integers – This value is represented by int class. It contains positive or negative whole numbers (without fraction or decimal). In Python there is no limit to how long an integer value can be.
- Float – This value is represented by float class. It is a real number with floating point representation. It is specified by a decimal point. Optionally, the character e or E followed by a positive or negative integer may be appended to specify scientific notation.
- Complex Numbers – Complex number is represented by complex class. It is specified as (real part) + (imaginary part)j. For example – 2+3j.
Sequence
- strings-
Strings in python are surrounded by either single quotation marks, or double quotation marks.
Example 'hello' is the same as "hello".
- Lists -
Python Lists are similar to arrays in C. However, the list can contain data of different types. The items stored in the list are separated with a comma (,) and enclosed within square brackets [].
We can use slice [:] operators to access the data of the list. The concatenation operator (+) and repetition operator (*) works with the list in the same way as they were working with the strings.
- Tuple-
Just like list, tuple is also an ordered collection of Python objects. The only difference between tuple and list is that tuples are immutable i.e. tuples cannot be modified after it is created. It is represented by tuple
class.