Python Variable and Types

Python Variable and Types

Variables are named location to hold a value that can be used throughout the program. Name of the variables and functions are called as identifiers.

Note : 1. Comment in Python starts with # (hash) character.

2. In Python, there is no need to use semicolon(;) at the end of the statement.

Assigning Value to a variable

  • In Python we do not need to declare a variable type.
  • It happens automatically when you will assign value to a variable.
  • Equal sign is used to assign value to a variable.

Below is the example of assigning values to variables:

[pastacode lang=”python” manual=”a%3D%20100%20%20%20%20%20%20%20%20%20%20%20%23a%20is%20integer%2C%20holds%20a%20value%20equal%20to%20100.%0Api%20%3D%203.14%20%20%20%20%20%20%20%20%23pi%20is%20float%0Astring1%3D%20%22I%20love%20programming%20in%20Python%22%20%20%20%23string1%20is%20a%20string%0Ax%3Dy%3Dz%3D%20100%20%20%20%20%20%20%20%20%20%20%20%23this%20statement%20assign%20100%20to%20z%2C%20y%20and%20x.%0Ap%2Cq%3D20%2C30%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23It%20will%20assign%2020%20to%20p%20and%2030%20to%20q.%0A%0A” message=”” highlight=”” provider=”manual”/]

Python Variable Types

Python has various standard data types and can be divided into following types.

  1. Number
  2. String
  3. List
  4. Tuple
  5. Dictionary
  6. Sets

Number in Python

Used to store numeric values.It stores four different types of numeric values.

Integer (Int) : like 34, 2, 67 etc.

Long : like 355487L , -4875684L, 83647L etc.

Float : like 54.00, 76.46 etc

Complex : 3.14j, 56.j, .645j etc.

Number objects are created when you assign a value to them. To define different types of numeric values see below code for reference.

[pastacode lang=”python” manual=”intvalue%20%3D%20%208%20%20%20%23define%20an%20integer%20value%0Amyfloat%20%3D%204.36%20%20%20%23define%20a%20float%20value%0Afloatval%20%3D%20float(7)%0Aa%20%3D%2010%0Ab%20%3D%2020%0Ax%2C%20y%20%3D%208.5%2C%202.4%0Aprint(%22Sum%20of%20a%20and%20b%20is%20%3A%22)%0Aprint(a%20%2B%20b)%0Aprint(%22Division%20of%20x%20and%20y%20is%3A%22)%0Aprint(x%2Fy)” message=”” highlight=”” provider=”manual”/]

Output of above code is :

Sum of a and b is :
30
Division of x and y is:
3.541666666666667

String in Python

String in python are set of characters. In python it can be defined either by \Single quote or Double quote.

Plus(+) sign is used for concatenation of strings and Multiplication(*) sign is used for repetition.

Below is the example of defining string object in python.

[pastacode lang=”python” manual=”str1%20%3D%20%22Sam%22%20%20%20%23%20using%20double%20quotes%0Astr2%20%3D%20’Joseph’%20%20%23%20using%20single%20quotes%0Aprint(%22My%20Name%20is%20%22%20%2B%20str1%20%2B%22%20%22%20%2B%20str2)%0Aprint(%22you%20can%20call%20me%20%22%20%2B%20(str1%5B0%5D%20*%202))%20%20%20%23printing%201st%20character%20of%20str1%20two%20times%0Aprint(%22My%20short%20name%20is%20%22%20%2B%20(str1%5B0%5D%20%2B%20str2%5B0%5D))%20%20%23printing%201st%20character%20of%20str1%20and%20str2%20together.” message=”” highlight=”” provider=”manual”/]

Output of above program :

My Name is Sam Joseph
you can call me SS
My short name is SJ

List in Python

  • It is the most famous data type of Python.
  • It is somewhat similar to array in C-language , one major difference is list stores heterogeneous data while array stores similar type of data.
  • A list contains items separated by comma(,) and enclosed in square bracket[]
  • We use the slicing operator [ ] to extract an item or a range of items from a list.
  • Index starts form 0 in Python.
  • List are mutable means, values can be modified.

List Example

[pastacode lang=”python” manual=”mylist%20%3D%20%5B%5D%20%20%20%23%20creates%20a%20empty%20list%0Amylist.append(10)%20%20%20%23%20add%2010%20to%20the%20empty%20list%0Amylist.append(30.6)%20%23%20add%2030.6%20to%20mylist%0Amylist.append(%22python%22)%20%20%23%20add%20string%20python%20to%20mylist%0Aprint(mylist%5B0%5D)%20%23%20prints%2010%0Aprint(mylist%5B1%3A2%5D)%20%23%20prints%2030.6%20and%20python%0Aprint(mylist)%20%20%20%20%23%20prints%20the%20whole%20list%0A%0Afor%20x%20in%20mylist%3A%0A%20%20%20%20print%20(x)%20%20%20%20%20%20%20%20%20%20%23%20prints%20out%20all%20value%20of%20list%20one%20by%20one” message=”” highlight=”” provider=”manual”/]

Below is the Output of above code :

10
[30.6]
[10, 30.6, 'python']
10
30.6
python

Tuple in Python

  • Tuple is very similar to List except its size is fixed, means we can not add or delete items from a Tuple.
  • Hence Tuple is immutable sequence type.

Tuple Example

[pastacode lang=”python” manual=”tuple1%20%3D%20(%20%22John%22%2C007%2C2.23%2C%20’Bond’%2C%2070.2%20%20)%0Atuple2%20%3D%20(123%2C%20’python’)%0A%0Aprint%20tuple1%20%20%20%20%20%20%20%20%20%20%20%23%20Prints%20the%20whole%20list%0Aprint%20tuple1%5B0%5D%20%20%20%20%20%20%20%20%23%20Prints%20first%20element%20of%20the%20list%0Aprint%20tuple1%5B1%3A4%5D%20%20%20%20%20%20%23%20Prints%20elements%20starting%20from%202nd%20till%203rd%20%0Aprint%20tuple1%5B2%3A%5D%20%20%20%20%20%20%20%23%20Prints%20elements%20starting%20from%203rd%20element%0Aprint%20tuple2%20*%202%20%20%20%20%20%20%20%23%20Prints%20two%20times%0Aprint%20tuple1%20%2B%20tuple2%20%20%23%20Prints%20concatenated%20lists” message=”” highlight=”” provider=”manual”/]

Output of above program:

('John', 7, 2.23, 'Bond', 70.2)
John
(7, 2.23, 'Bond')
(2.23, 'Bond', 70.2)
(123, 'python', 123, 'python')
('John', 7, 2.23, 'Bond', 70.2, 123, 'python')

Dictionary in Python

  • Dictionary in python stores values in key-value pairs.
  • It is used to fast retrieve, add, remove and modify the values using the key.
  • It is somewhat similar to associative array or Hashes.
  • Dictionaries are enclosed by curly braces ({ }) and values can be assigned and accessed using square braces ([]).

Dictionary Example

[pastacode lang=”python” manual=”%23%20Create%20a%20dictionary.%0Adata%20%3D%20%7B%22a%22%3A%201%2C%20%22b%22%3A%202%2C%20%22c%22%3A%203%7D%0A%0A%23%20Loop%20over%20items%20and%20unpack%20each%20item.%0Afor%20k%2C%20v%20in%20data.items()%3A%0A%20%20%20%20%23%20Display%20key%20and%20value.%0A%20%20%20%20print(k%2C%20v)” message=”Program to create and display a dictionary” highlight=”” provider=”manual”/]

3 Replies to “Python Variable and Types”

Leave a Reply