Python Programming Notes and Practical Programs BCA 3rd Semester CSJM University Kanpur (Lecture 3)

Python Programming Notes and Practical Programs BCA 3rd Semester CSJM University Kanpur

Python Variables with examples

Variables are used to store data, they take memory space based on the type of value we assigning to them. Creating variables in Python is simple, you just have write the variable name on the left side of = and the value on the right side, as shown below.

Num = 20     #num is of type int
str = "Ankit"      #str is of type string

Variable name – Identifiers

Variable name is known as identifier. There are few rules that you have to follow while naming the variables in Python.
1. The name of the variable must always start with either a letter or an underscore (_). For example: _str, str, num, _num are all valid name for the variables.
2. The name of the variable cannot start with a number. For example: 9num is not a valid variable name.
3. The name of the variable cannot have special characters such as %, $, # etc, they can only have alphanumeric characters and underscore (A to Z, a to z, 0-9 or _).
4. Variable name is case sensitive in Python which means 
num and NUM are two different variables in python.

Python Variable Example

num = 100
str = "BeginnersBook"
print(num)
print(str)

 

Output:
100

BeginnersBook

 

 

Python multiple assignment

We can assign multiple variables in a single statement like this in Python.

x = y = z = 99
print(x)
print(y)
print(z)

 

Output:

99

99

99

Another example of multiple assignment

a, b, c = 5, 6, 7
print(a)
print(b)
print(c)

Output:
5

6

7

Plus and concatenation operation on the variables

x = 10
y = 20
print(x + y)
 
p = "Hello"
q = "World"
print(p + " " + q)

Output:

30

Hello World

However if you try to use the + operator with variable x and p then you will get the following error.

unsupported operand type(s) for +: 'int' and 'str'

Data Types

A data type defines the type of data, for example 123 is an integer data while “hello” is a String type of data. The data types in Python are divided in two categories:
1. Immutable data types – Values cannot be changed.
2. Mutable data types – Values can be changed

Immutable data types in Python are:
1. 
Numbers
2. 
String
3. 
Tuple

Mutable data types in Python are:
1. List
2. 
Dictionaries
3.
Sets

1. Numeric Data Type in Python

Integer – In Python 3, there is no upper bound on the integer number which means we can have the value as large as our system memory allows.

# Integer number
num = 100
print(num)
print("Data Type of variable num is", type(num))

Output:
100

Data Type of variable num is <class ‘int’>

 

Long – Long data type is deprecated in Python 3 because there is no need for it, since the integer has no upper limit, there is no point in having a data type that allows larger upper limit than integers.

Float – Values with decimal points are the float values, there is no need to specify the data type in Python. It is automatically inferred based on the value we are assigning to a variable. For example here fnum is a float data type.

# float number
fnum = 34.45
print(fnum)
print("Data Type of variable fnum is", type(fnum))

Output:

34.45

Data Type of variable fnum is <class ‘float’>



Complex Number – Numbers with real and imaginary parts are known as complex numbers. Unlike other programming language such as Java, Python is able to identify these complex numbers with the values. In the following example when we print the type of the variable cnum, it prints as complex number.

# complex number
cnum = 3 + 4j
print(cnum)
print("Data Type of variable cnum is", type(cnum))

Output:

(3+4j)

Data Type of variable cnum is <class ‘complex’>

Binary, Octal and Hexadecimal numbers

In Python we can print decimal equivalent of binary, octal and hexadecimal numbers using the prefixes.
0b(zero + ‘b’) and 0B(zero + ‘B’) – Binary Number
0o(zero + ‘o’) and 0O(zero + ‘O’) – Octal Number
0x(zero + ‘x’) and 0X(zero + ‘X’) – Hexadecimal Number

# integer equivalent of binary number 101
num = 0b101
print(num)
# integer equivalent of Octal number 32
num2 = 0o32
print(num2)
# integer equivalent of Hexadecimal number FF
num3 = 0xFF
print(num3)

 

Output:

5

26

255

2. Python Data Type – String

String is a sequence of characters in Python. The data type of String in Python is called “str”.

Strings in Python are either enclosed with single quotes or double quotes. In the following example we have demonstrated two strings one with the double quotes and other string s2 with the single quotes.

# Python program to print strings and type
 
s = "This is a String"
s2 = 'This is also a String'
 
# displaying string s and its type
print(s)
print(type(s))
 
# displaying string s2 and its type
print(s2)
print(type(s2))

Output:

This is a String

<class ‘str’>

This is also a String

<class ‘str’>

3. Python Data Type – Tuple

Tuple is immutable data type in Python which means it cannot be changed. It is an ordered collection of elements enclosed in round brackets and separated by commas.

# tuple of integers
t1 = (1, 2, 3, 4, 5)
# prints entire tuple
print(t1)                       
 
# tuple of strings
t2 = ("hi", "hello", "bye")
# loop through tuple elements
for s in t2:
    print (s)
 
# tuple of mixed type elements
t3 = (2, "Lucy", 45, "Steve")
'''
Print a specific element
indexes start with zero
'''
print(t3[2])

Output:

(1, 2, 3, 4, 5)

Hi

Hello

Bye

45

4. Python Data Type – List

List is similar to tuple, it is also an ordered collection of elements, however list is a mutable data type which means it can be changed unlike tuple which is an immutable data type.

A list is enclosed with square brackets and elements are separated by commas.

# list of integers
lis1 = [1, 2, 3, 4, 5]
# prints entire list
print(lis1)
 
# list of strings
lis2 = ["Apple", "Orange", "Banana"]
# loop through list elements
for x in lis2:
    print (x)
 
# List of mixed type elements
lis3 = [20, "csa ", 15, "csaankit"]
'''
Print a specific element in list
indexes start with zero
'''
print("Element at index 3 is:",lis3[3])

 

 

Output:

[1, 2, 3, 4, 5]

Apple

Orange

Banana

Element at index 3 is: csaankit

5. Python Data Type – Dictionary

Dictionary is a collection of key and value pairs. A dictionary doesn’t allow duplicate keys but the values can be duplicate. It is an ordered, indexed and mutable collection of elements.

The keys in a dictionary doesn’t necessarily to be a single data type, as you can see in the following example that we have 1 integer key and two string keys.

# Dictionary example
 
dict = {1:"Ankit","lastname":"Shukla", "age":28}
 
# prints the value where key value is 1
print(dict[1])
# prints the value where key value is "lastname"
print(dict["lastname"])
# prints the value where key value is "age"
print(dict["age"])

Output:

Ankit

Shukla

28

6. Python Data Type – Set

A set is an unordered and unindexed collection of items. This means when we print the elements of a set they will appear in the random order and we cannot access the elements of set based on indexes because it is unindexed.

Elements of set are separated by commas and enclosed in curly braces. Lets take an example to understand the set.

# Set Example
myset = {"hi", 2, "bye", "Hello World"}
 
# loop through set
for a in myset:
    print(a)
 
# checking whether 2 exists in myset
print(2 in myset)
 
# adding new element
myset.add(99)
print(myset)

Output:

hi

2

bye

Hello World

True

{'hi', 2, 99, 'Hello World', 'bye'}

Topic :Software & Types, Subject: Computer Fundamental Notes for CSJM University Kanpur(for different courses like BBA, BCA, etc..)

Software Software refers to the programs, data, and instructions that enable a computer or other digital device to perform specific tasks or...