Python Variables


Variables:

  • Variables are named locations in memory that are used to hold a value that may be modified by the program.

  • A variable may take different values at different time during execution.

  • A variable name must be a valid identifier, means can be a group of both letters and digits, but they have to begin with a letter or an underscore.


Variable name should not be a keyword.


To understand variable, you can assume that variable is like a container that holds data.


Declaring Variable and Assigning Values

  • In Python, You don't need to specify the type of variable because Python is a type infer language.

  • You don't need to declare explicitly variable in Python. When you assign any value to the variable that variable is declared automatically.

  • Syntax :

    variable_name = value
    for e.g.
    name = "prowessapps"
    age  = 20
    salary = 20000
    tax_rate = 4.5
  • As you assign value to the variable, that variable becomes of that type.

  • To check the type of any variable you can use inbuilt function type ( ).

type ( ) Function :

>>> name = "prowessapps"
>>> type(name)
<class 'str'>  type of 'name' is 'string'

>>> age = 20
>>> type(age)
<class 'int'>  type of 'age' is 'int'

>>> rate = 4.5
>>> type(tax_rate)
<class 'float'>  type of 'rate' is 'float'
>>>


Python is dynamically typed language.

Meaning of dynamically typed :

Dynamically type means you can use same variable for different value in the same program.

As per your passed value to the variable, it will become of that type.

>>> a = 20
>>> type(a)
<class 'int'>  here 'a' is 'int'

>>> a = 2.3
>>> type(a)
<class 'float'>  now 'a' is 'float'

>>> a = "Hello"
>>> type(a)
<class 'str'>  now 'a' is 'string'

Multiple Assignment

Assigning single value to multiple variables :
#script.py

x=y=z=50
print(x)
print(y)
print(z)
OUTPUT :
50
50
50

Assigning multiple values to multiple variables:
#script.py

x, y, z = 50, 2.3, "Hello"
print(x)
print(y)
print(z)
OUTPUT :
50
2.3
Hello

Next chapter is python data types





 



Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in


CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved