DEV Community

Cover image for Python Foundation with Data Structures & Algorithms - Part 02
NirmaniWarakaulla
NirmaniWarakaulla

Posted on • Updated on

Python Foundation with Data Structures & Algorithms - Part 02

Python Variables

Variables are containers for storing data values. Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.

The interpreter allocates memory and determines what can be stored in the allocated memory depending on the data type of a variable. As a consequence, you can store integers, decimals, or characters in variables by assigning various data types to them.

Every value in Python has a datatype. Different data types in Python are Numbers, List, Tuple, Strings, Dictionary, etc. Variables in Python can be declared by any name or even alphabets like a, aa, abc, etc.

Assigning Values to Variables

 x = 10
 y = "MachineLearning.org.in"
 z = 3.14
 print(x)
 print(y)
 print(z)
Enter fullscreen mode Exit fullscreen mode

10
MachineLearning.org.in
3.14

Variables do not need to be declared with any particular type, and can even change type after they have been set.

 x = 10
 x = "MachineLearning.org.in"
 print(x)
Enter fullscreen mode Exit fullscreen mode

MachineLearning.org.in

Multiple Assignment
 a = b = c = 10
 print(a,b,c)
Enter fullscreen mode Exit fullscreen mode

10 10 10

 a,b,c = 10,3.14,"MachineLearning.org.in"
 print(a)
 print(b)
 print(c)
Enter fullscreen mode Exit fullscreen mode

10
3.14
MachineLearning.org.in

Type Casting
 x = str(3)  
 print(x)

 y = int(3) 
 print(y)

 z = float(3)
 print(z)
Enter fullscreen mode Exit fullscreen mode

3
3
3.0

Get the Type
 x = str(3)  
 print(type(x))

 y = int(3) 
 print(type(y))

 z = float(3)
 print(type(z))
Enter fullscreen mode Exit fullscreen mode
 n = input("Enter any value:")
 print(n)
 print(type(n))
Enter fullscreen mode Exit fullscreen mode

Enter any value:Machine Learning
Machine Learning

 n = int(input("Enter any value:"))
 print(n)
 print(type(n))
Enter fullscreen mode Exit fullscreen mode

Enter any value:10
10

 n = float(input("Enter any value:"))
 print(n)
 print(type(n))
Enter fullscreen mode Exit fullscreen mode

Enter any value:3.14
3.14

 n = str(input("Enter any value:"))
 print(n)
 print(type(n))
Enter fullscreen mode Exit fullscreen mode

Enter any value:Machine Learning
Machine Learning

Concatenation
 x = "Learning"
 print("Machine " + x)
Enter fullscreen mode Exit fullscreen mode

Machine Learning

 x = "Machine "
 y = "Learning "
 z =  x + y
 print(z)
Enter fullscreen mode Exit fullscreen mode

Machine Learning

Python has five standard data types −

Numbers
String
List
Tuple
Dictionary

1. Number
 a = 10
 print(a)
Enter fullscreen mode Exit fullscreen mode

10

delete number
 del a

 print(a)
Enter fullscreen mode Exit fullscreen mode

NameError: name 'a' is not defined

2. String
 s = "MachineLearning.org.in"
 print(s)           # Prints complete string
 print(s[0])        # Prints first character of the string
 print(s[7:15])     # Prints characters starting from 3rd to 5th
 print(s[7:])       # Prints string starting from 3rd character
 print(s * 2)       # Prints string two times
 print('www.' + s)  # Prints concatenated string
 print(s[:])        # Prints complete string with range start to end
 print(s[::])       # Prints complete string with range start to end with increment 1
 print(s[::2])      # Print String with increment of 2
 print(s[-1])       # print last character of a string
 print(s[-3:])      # print last 3 character of a string
Enter fullscreen mode Exit fullscreen mode

MachineLearning.org.in
M
Learning
Learning.org.in
MachineLearning.org.inMachineLearning.org.in
www.MachineLearning.org.in
MachineLearning.org.in
MachineLearning.org.in
Mcieerigogi
n
.in

3. List
      L = [10,3.14,"Machine",45,'Learning',78.98,69,'.org.in']
      print(L)
      print(L[:])
      print(L[::])
      print(L[0])
      print(L[2:5])
      print(L[2:])
      print(L[:5])
      print(L[-1])
      print(L[-3:])
Enter fullscreen mode Exit fullscreen mode

[10, 3.14, 'Machine', 45, 'Learning', 78.98, 69, '.org.in']
[10, 3.14, 'Machine', 45, 'Learning', 78.98, 69, '.org.in']
[10, 3.14, 'Machine', 45, 'Learning', 78.98, 69, '.org.in']
10
['Machine', 45, 'Learning']
['Machine', 45, 'Learning', 78.98, 69, '.org.in']
[10, 3.14, 'Machine', 45, 'Learning']
.org.in
[78.98, 69, '.org.in']

  L1 = [10,20,30]
  L2 = [40,50,60]
  print(L1 + L2)
Enter fullscreen mode Exit fullscreen mode

[10, 20, 30, 40, 50, 60]

  L1 = [10,20,30]
  print(L1 * 3)
Enter fullscreen mode Exit fullscreen mode

[10, 20, 30, 10, 20, 30, 10, 20, 30]

4. Tuples
  T = (10,3.14,"Machine",45,'Learning',78.98,69,'.org.in')
  print(T)
  print(T[:])
  print(T[::])
  print(T[0])
  print(T[2:5])
  print(T[2:])
  print(T[:5])
  print(T[-1])
  print(T[-3:])
Enter fullscreen mode Exit fullscreen mode

(10, 3.14, 'Machine', 45, 'Learning', 78.98, 69, '.org.in')
(10, 3.14, 'Machine', 45, 'Learning', 78.98, 69, '.org.in')
(10, 3.14, 'Machine', 45, 'Learning', 78.98, 69, '.org.in')
10
('Machine', 45, 'Learning')
('Machine', 45, 'Learning', 78.98, 69, '.org.in')
(10, 3.14, 'Machine', 45, 'Learning')
.org.in
(78.98, 69, '.org.in')

  T1 = [10,20,30]
  T2 = [40,50,60]
  print(T1 + T2)
Enter fullscreen mode Exit fullscreen mode

[10, 20, 30, 40, 50, 60]

  T1 = [10,20,30]
  print(T1 * 3)
Enter fullscreen mode Exit fullscreen mode

[10, 20, 30, 10, 20, 30, 10, 20, 30]

5. Dictionary
  d = {}
  print(d)
Enter fullscreen mode Exit fullscreen mode

{}

  d = {'Name':'Kevin','Age':34,'Marks':89.58}
  print(d)
  print(d['Name'])
  print(d['Age'])
  print(d['Marks'])
Enter fullscreen mode Exit fullscreen mode

{'Name': 'Kevin', 'Age': 34, 'Marks': 89.58}
Kevin
34
89.58

  print(d.keys())
  print(d.values())
  print(d.items())
Enter fullscreen mode Exit fullscreen mode

dict_keys(['Name', 'Age', 'Marks'])
dict_values(['Kevin', 34, 89.58])
dict_items([('Name', 'Kevin'), ('Age', 34), ('Marks', 89.58)])

Top comments (0)