Python Variables :

What is Variables?

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

Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals or characters in these variables.

Creating 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. For example :

name="AyaN"; roll=21; print(name); print(age);

Output :

python creating variables

Note : Here name is a string and age is a integer.

Python allows you to assign a single value to several variables simultaneously (Multiple Assignment). For example :

a=b=c=1; print(a); print(b); print(c);
Or,
a,b,c=1,2,"ayan"; print(a); print(b); print(c);

python creating  variables

Note : Here you can see Two errors cause - 1st error (NameError) : when we add a string vale must add " " (Example: c="your string"). 2nd Error (SyntaxError) : must add ( , ) when adding multiple values (Example: a,b,c=1,2,3 or a,b,c=1,2,"your string")


Standard Data Types :

The data stored in memory can be of many types. For example, a person's age is stored as a numeric value and his or her address is stored as alphanumeric characters. Python has various standard data types that are used to define the operations possible on them and the storage method for each of them. Python standard data types : Numbers, String, List, Tuple, Sets, Dictionary.


Python Numbers :

Number data types store numeric values. Number objects are created when you assign a value to them. For example : a=2; b=3.8; c=10j;

Python supports four different numerical types :

intfloatcomplex
100.03.14j
10015.2045.j
-786-21.99.322e-36j
08032.3+e18.876j
-0490-90.-.6545+0J
-0x260-32.54e1003e+26J
0x6970.2-E124.53e-7j

Example :

  1. a = 2 #int
  2. b = 3.8 #float
  3. c = 10j #complex
  4. print(type(a))
  5. print(type(b))
  6. print(type(c))
  7. input()

Output :

python number examples

Python Strings :

Strings in Python are identified as a contiguous set of characters represented in the quotation marks. Python allows for either pairs of 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 and working their way from -1 at the end.

The plus ( + ) sign is the string concatenation operator and the asterisk ( * ) is the repetition operator.

Example :

  1. str="Hello, Python!"
  2. print(str) #Prints complete string
  3. print(str[0]) #Prints first character of the string
  4. print(str[2:9]) #Prints characters starting from 3rd to 9th
  5. print(str[2:]) #Prints string starting from 3rd character
  6. print(str*2) #Prints string two times
  7. print(str+"Another Text") #Prints concatenated strings
  8. input()

Output :

python string examples

Python Lists :

Lists are the most versatile of Python's compound data types. A list contains items separated by commas and enclosed within square brackets ( [ ] ). To some extent, lists are similar to arrays in C. One difference between them is that all the items belonging to a list can be of different data type.

The values stored in a list can be accessed using the slice operator ( [ ] and [:] ) with indexes starting at 0 in the beginning of the list and working their way to end -1. The plus ( + ) sign is the list concatenation operator, and the asterisk ( * ) is the repetition operator.

Example :

  1. list=["AyaN",24,786,"$250,0000","Ray",22,786,"$135,000"]
  2. anotherlist=[10.5,"work"]
  3. print(list) #Prints complete list
  4. print(list[0]) #Prints first element of the list
  5. print(list[1:5]) #Prints elements starting from 2nd till 4th
  6. print(list[2:]) #Prints elements starting from 3rd element
  7. print(anotherlist * 2) #Prints list two times
  8. print(list+anotherlist) #Prints concatenated lists
  9. input()

Output :

python_lists_examples

Python Tuples :

A tuple is another sequence data type that is similar to the list. A tuple consists of a number of values separated by commas. Unlike lists, however, tuples are enclosed within parentheses.

The main differences between lists and tuples are: Lists are enclosed in brackets ( [ ] ) and their elements and size can be changed, while tuples are enclosed in parentheses ( ( ) ) and cannot be updated. Tuples can be thought of as read-only lists.

Example :

  1. tuple=("AyaN",24,786,"$250,0000","Ray",22,786,"$135,000")
  2. anothertuple=(10.5,"work")
  3. print(tuple) #Prints complete list
  4. print(tuple[0]) #Prints first element of the list
  5. print(tuple[1:5]) #Prints elements starting from 2nd till 4th
  6. print(tuple[2:]) #Prints elements starting from 3rd element
  7. print(anothertuple * 2) #Prints list two times
  8. print(tuple+anothertuple) #Prints concatenated lists
  9. input()

Output :

python tuple examples

Python Sets :

A set is a collection which is unordered and unindexed. In Python sets are written with curly brackets. Sets are enclosed by curly braces ( { } ) .

Example :

  1. thisset = {"ayan", "techbaz", "2019", "another", "lists"}
  2. print(thisset) #prints all values
  3. for x in thisset:
  4. print(x) #prints all value use for loop
  5. print("\n-----") #we create a new line
  6. print("techbaz" in thisset) #Check if "techbaz" is present in the set
  7. input()

Output :

python sets examples

Python Dictionary :

Python's dictionaries are kind of hash table type. They work like associative arrays or hashes found in Perl and consist of key-value pairs. A dictionary key can be almost any Python type, but are usually numbers or strings. Values, on the other hand, can be any arbitrary Python object.

Dictionaries are enclosed by curly braces ( { } ) and values can be assigned and accessed using square braces ( [ ] ).

Example :

  1. dicto={}
  2. dicto["first"]="This is First"
  3. dicto[2]="This is Second"
  4. another_dicto={"name":"ayan","age":"24","Occupation":"Programmer"}
  5. print(dicto["first"]) #Prints value for "first" key
  6. print(dicto[2]) #Prints value for 2 key
  7. print(another_dicto) #Prints full dictionary
  8. print(another_dicto.keys()) #Prints all the keys
  9. print(another_dicto.values()) #Prints all the values
  10. input()

Output :

python dictionary examples

Computer Science Engineering

Special Notes

It's a special area where you can find special questions and answers for CSE students or IT professionals. Also, In this section, we try to explain a topic in a very deep way.

CSE Notes