Variables, keywords and datatypes in python
A variable is acts like a container which holds some specific value and a datatypes indicates which type of data does the variable holds either it is a real or integer or any alphabetical character .
Variables :
so instead of writing 2 + 4 , you can use
x = 2 , y=4
x+y
So why we need variables ?
variables concept is common in every programming language and declaring a
variables is beneficial for you program for passing data as arguments or save
the data for future use with in a program scope .
Data types :
Their are 3 Basic data type in python.
1 . integer (all the integer number comes into it like 1,2,3,4,5,etc)
2. float (all the real numbers like 12.4 ,56.9,2.0 ,etc)
3. string (all alphabet and words 'a',"B",'hello' but in single
or double quotes.)
How we define which variable holds which type of data ?
In python it is very easy to declare a variable .
x = 5 ( x is a integer type variable which holds value 5 )
y =23.5 (y is floating type variable which holds value 23.5 )
z = " hello " (z is a string type variable which holds value
"hello" )
Python automatically checks which variable holds which type of data so you don't need to declare it like other programming languages.
Now lets play with variables.
>>> a = 10
>>> b = 20
>>> c = a+b
>>> c
30
>>> d = a + b + c
>>> d
60
>>> a = 'hello'
>>> a
'hello'
>>> a = 12.5
>>> a
12.5
>>>
In the above code we use a variable a for storing a integer type data ,
after string and at last float.
This is an example of why python is dynamically typed.
We can use a single variable foe storing different type of data but one at
each time.
For declaring a variable we use assignment operator . so lets briefly understand it .
Assignment Operators
Refer as the name suggests is used to declare assignments to the operands,
following are the types of assignment operators in python.
Operator Description Syntax Output
= Equal to c = a + b assigns a value of a + b into c
+= Add AND c += a is equivalent to c = c + a
-= Subtract AND c -= a is
equivalent to c = c – a
*= Multiply AND c *= a is
equivalent to c = c * a
/= Divide AND c /= a is
equivalent to c = c / ac /= a is equivalent to c = c / a
%= Modulus AND c %= a is
equivalent to c = c % a
Multiple assignments.
we can also assign all the variable in single line .
>>> var1 , var2 , var3 = 1,2,3
>>> var1
1
>>> var2
2
>>> var3
3
>>> var1 = var2 = 2
>>> var1
2
>>> var2
2
>>>
type ( ) function.
type is a per defined function of python used which returns the class/
datatype of a variable .
but wait what is a function :
In simple words a function is a set of code and the term pre defined means
it is a built-in function which code is written some where in our python
libraries.
lets me show you by coding.
>>> a = 12
>>> type(a)
>>> a = "hello"
>>> type(a)
>>>
<class 'int'>
<class 'str'>
because of some error with bloggers we can't show the output inside the code area .
Now lets move on our topic
as you can see the type function return a the type of variable according to the variable .
but it is also returns " class " what does it means ?
Hold on we will discuss it in python Object and classes section briefly.
Points to remember :
- A variable can change , or vary as code executes .
- A data types defines the type of data which a variables holds.
Keywords .
Keywords are the name that should not be used for variable names. keywords are the reserved words which is assigned to a specific task.
their are 33 keywords available with python.
The Python Keywords are
True | False | class | def | return |
if | elif | else | try | except |
raise | finally | for | in | is |
not | from | import | global | lambda |
nonlocal | pass | while | break | continue |
and | with | as | yield | del |
or | assert | None |
Rules of Naming Variables.
- A variable name must begin with a letter or underscore ' _ '.
- A variable name can't contain spaces, punctuations, or special characters other than the underscore.
- A variable name can't begin with a number, but can use number later in name.
- A variable name can't be the same as a keyword that's reserved by python.
- Start all variable names with a lowercase letter.
- Use underscore notation or camel case.
- Use meaningful names that are easy to remember
- Don't use any keyword name.
No comments