02 Variables and Types

Variable are used to store  values in memory location. By creating variables one reserve some space in memory. By assigning different data types to variables, you can store integers, decimals or characters in these variables.


Assigning Values to Variables:

Python variables do NOT need explicit declaration to reserve memory space. The declaration happens automatically when you assign a value to a variable. The equal sign (=) is used to assign values to variables. Every variable in Python is an object. Let's take a look of the following example.

car = 100                # An integer assignment
miles = 1000.37     # A floating point
name = "John"       # A string, remember that for string, we used ""

print (car)
print (miles)
print (name)

In python shell you will see following output,



Python Strings:

String in Python is identified as a contiguous set of characters represented in the quotation marks. One can use single or double quotes. Subsets of strings can be taken using the slice operator ([ ] and [:] ) with indexes starting at 0 in the beginning of the string.

# Let's talk about strings

str = "Welcome to Learn Python!"

print (str)             # Prints complete string 
print (str[0])        # Prints first character of the string 
print (str[3:6])     # Prints characters starting from 4th to 6th 
print (str[3:])       # Prints string starting from 4th character 
print (str * 2)       # Prints string two times 
print ("You are, "+ str)     # Prints concatenated string

In python shell you will see following output,