0% found this document useful (0 votes)
7 views10 pages

Basic Variables and DataTypes

The document provides an overview of basic data types and variables in Python, including print statements, comments, variables, constants, literals, and various data structures such as lists, tuples, sets, and dictionaries. It explains how to define and manipulate these data types with examples. Additionally, it covers the characteristics and methods associated with each data type.

Uploaded by

manipolaki2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views10 pages

Basic Variables and DataTypes

The document provides an overview of basic data types and variables in Python, including print statements, comments, variables, constants, literals, and various data structures such as lists, tuples, sets, and dictionaries. It explains how to define and manipulate these data types with examples. Additionally, it covers the characteristics and methods associated with each data type.

Uploaded by

manipolaki2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Welcome to

INTERNSHIP STUDIO
Module 02 | Lesson 02
Basic data types and
variables

WWW.INTERNSHIPSTUDIO.COM
Print, Comment, variables, constant, literals

Print

•print("Hello Python")
•print('Hello Python')

WWW.INTERNSHIPSTUDIO.COM
Print, Comment, variables, constant, literals

Comment

•# is used to comment single line and "" """


is used to define multiline comment
#print("Hello Python")
•print('Hello Python') # comment
•""" This is an
example of
multi line comment
"""

WWW.INTERNSHIPSTUDIO.COM
Print, Comment, variables, constant, literals

Variable

whose values may change during the program


a=5
a=10
a=100
name="Avinash"
name="Naveen"
name="Neha"

WWW.INTERNSHIPSTUDIO.COM
Print, Comment, variables, constant, literals

Constant
whose values are fixed during the program, there is no keyword to define the
constant they are mostly written in capital letters
A=5
NAME="Avinash"

Literals
is a raw data assigned to either variable or constant
a = 0b1010 #Binary Literals # literals
b = 100 #Decimal Literal a = 0b1010 #Binary Literals
c = 0o310 #Octal Literal b = 100 #Decimal Literal
d = 0x12c #Hexadecimal Literal c = 0o310 #Octal Literal
d = 0x12c #Hexadecimal Literal
#Float Literal print(a,b,c,d)
float_1 = 10.5 10 100 200 300
float_2 = 1.5e2
# String Literal

multiline_str = """This is a multiline string with more than one word."""


unicode = u"\u00dcnic\u00f6de"

WWW.INTERNSHIPSTUDIO.COM
Data Types

Name Definition Meaning


Integer consist of integer values a=5
print(a)
5

Float consist of floating point A=15.2


values Print(A)
15.2

String consist of text, and alpha name="Avinash“


numeric values Print(name)
Avinash

WWW.INTERNSHIPSTUDIO.COM
Data Types
List:
just as array in C
List items are ordered, changeable, and allow duplicate values.
When we say that lists are ordered, it means that the items have a defined order, and that order will not
change.
The list is changeable, meaning that we can change, add, and remove items in a list after it has been created.
List items are ordered, changeable, and allow duplicate values.
# Example
a = ("apple", "banana", "cherry")
# Access list element
print(a[0],a[1])
# Change list item
a[1] = "blackcurrant"
# Add item in the list
a.append("orange")
# Remove item from the list
a.remove("banana")
# Sort list in ascending order
a.sort()
# copy list
b=a # this will copy the reference of list a into b, hence if you change the list a the change would impact in
list b
# Join two lists
b = (1 , 2, 3)
c=a+b

WWW.INTERNSHIPSTUDIO.COM
Data Types
List:

# Methods:
1. count, count the number of occurrence of the element in the list | Example: a.count("apple")
2. index, find out the index of the element | Example: a.index("banana")
3. extend, add the elements of b in a| Example: a.extend(b)
4. reverse, reverse the order of the list | Example: a.reverse()

Touple:
used to store multiple items in a single variable.
A tuple is a collection which is ordered and unchangeable.
# Example
a = ("apple", "banana", "cherry")
# Access touple element
print(a[0],a[1])
# Add two touple
b = (1 , 2, 3)
c=a+b

# Methods:
1. count, count the number of occurrence of the element in the touple | Example: a.count("apple")
2. index, find out the index of the element | Example: a.index("banana")

WWW.INTERNSHIPSTUDIO.COM
Data Types
Set:
used to store multiple items, unlike tuple it is both unordered and unindexed. Set items are unordered,
unchangeable, and do not allow duplicate values.
1- Unordered means that the items in a set do not have a defined order.
2- Sets are unchangeable, meaning that we cannot change the items after the set has been created.
3- Sets cannot have two items with the same value.
# Example
a = {"apple", "banana", "cherry"}
print(a)
# Duplicate values will be ignored
a = {"apple", "banana", "cherry", "apple"}
print(a) # {"apple", "banana", "cherry"}
# Add element in the set
a.add("orange")
# Remove item from the set
a.remove("banana")
# union of two sets
b = {1,2,3}
a.union(b)
# Methods:

1. pop # remove element from the set, Example: a.remove("banana")


2. difference # to take the difference of two sets
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.difference(y)
print(z) # {'cherry', 'banana'}

WWW.INTERNSHIPSTUDIO.COM
Data Types
Dictionary:
Stores the data in key and value pairs
Dictionary items are ordered, changeable, and does not allow duplicates.
# Example
a = { "brand": "Ford", "model": "Mustang", "year": 1964 }
# Access dictionary element
print(a["model"])
# Change the element value
a["year"] = 2018
# Add item in the dictionary
a["color"] = "red"
# remove item from the dictionary
a.pop("model")
del a["model"]
# copy dictionary, this is same as list
b = a
# Methods:

1. keys, return all keys of the dictionay | Example: a.keys()


2. values, return all values of the dictionary | Example: a.values()

Bool: it is represented by the variable "bool" and it can have only two values (true or False)

WWW.INTERNSHIPSTUDIO.COM

You might also like