Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

Python Programming Powerpoint v[2]

Uploaded by

santhosh13824
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Python Programming Powerpoint v[2]

Uploaded by

santhosh13824
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

PYTHON PROGRAMMING

UNIT -1

BY…..
AISHWARYA.D
Python:

 Numbers

 Strings

 Variables

 Lists

 Tuples

 Dictionaries

 Sets

 Comparison
Data Structure in Python
NUMBERS
In Python, numbers are a fundamental data type used to represent numerical
values.
 Here's a breakdown of the different types of numbers in Python:
1.Integers (int): Represent whole numbers without any fractional component.
Can be positive, negative, or zero.
Examples: 1, -10, 0, 1000000.
2. Floating-Point Numbers (float):
Represent real numbers with a decimal point.
Can be positive, negative, or zero.
Examples: 3.14, -2.718, 0.0.
3. Complex Numbers (complex):
Represent numbers with a real and an imaginary part.
Written in the form a + bj, where a is the real part and b is the imaginary part
and j represents the imaginary unit (√-1).

Key Points about Numbers in Python:

Arithmetic Operations:
 Python supports standard arithmetic operations on numbers,
including addition (+), subtraction (-), multiplication (), division (/),
modulo (%), and exponentiation (*).
STRINGS
String is a collection of one or more character put in a single code, double
code or triple code.
 python where is a character data type a character is a string of length
one it is represent by string class.
Example:
String 1 = ‘welcome’
print(“string with the use of single quotes”)
print(string 1)
String 1 =“welcome”
print(“\n string with use of double quotes”)
print(type (string 1))
String 1 =‘“Welcome’’’
print(“\n string with use of Triple quotes”)
print(string 1)
print(type(string 1))
Output:

‘welcome’
“welcome”
‘“welcome’”

Example
1. str = "string using double quotes"
2. print(str)
3. s = '''''A multiline
4. string'''
5. print(s)

Output:

string using double quotes


A multiline
String
VARIABLES
A variable in Python is a container that stores data values.
Variable are reserved memory location used to store value.
Variables can store many types of data, including integers, strings,
lists, and dictionaries.

Creating Variables:
Ex: x=5
y=“Hi”
print(x)
print(y)
x=6
print(x)
Output:

Hi

String Variables:

String variables it can be declare either by using single or double qoutes.

Ex: x=“John”
x=‘John’
Case Sensitive:

Python variable is case sensitive.

Ex: name = “Hi”

Name = “Hello”

Ex: a= 10

b= 5

print(a + b)

Output:

15
Rules for python variable:

Python variable name start with letter or underscore characters.

Python variable name can’t start with a digits.

Python variable name can only contain alpha numeric characters and
Underscore.

Variables in python are case sensitive.

Keywords can’t used as python variables.

Python Variable Types:

 Local Variable

 Global Variable
Local Variable:

The variable that are declare with in a function we can’t access variabl
outside the function.

Ex:

def f():
# local variable
s = "I love Geeksforgeeks“
print(s)
# Driver code f()

Output:

I love Geeksforgeeks

Global Variable:

Variable created outside a function can be accessed with in a function o


outside the function.
Ex:

# This function uses global variable s


def f():
print("Inside Function", s)
# Global scope
s = "I love Geeksforgeeks"
f()
print("Outside Function", s)

Output

Inside Function I love Geeksforgeeks


Outside Function I love Geeksforgeeks
List
 List is just like the array which is order to collection of data.

 It is very flexible at the item in a list don’t need to be same type.

 List items are ordered, changeable, and allow duplicate values.

Example:

Create a List:

thislist = ["apple", "banana", "cherry"]

print(thislist)

Output:

['apple', 'banana', 'cherry']


Access Items:

List items are indexed and you can access them by referring to
the index number.

Example:

Print the second item of the list:

thislist = ["apple", "banana", "cherry"]

print(thislist[1])

Output:
Banana

Change Item Value:

To change the value of a specific item, refer to the index number:


Example:

Change the second item:

thislist =
["apple", "banana", "cherry"]

thislist[1] = "blackcurrant“

print(thislist)

Output:

['apple', 'blackcurrant', 'cherry']


Add List Items:

Append Items:

To add an item to the end of the list, use the append() method:

Example:

Using the append() method to append an item:

thislist = ["apple", "banana", "cherry"]


thislist.append("orange")
print(thislist)

Output:

['apple', 'banana', 'cherry', 'orange']


Remove Specified Item:

The remove() method removes the specified item.

Example:

Remove "banana":

thislist = ["apple", "banana", "cherry"]


thislist.remove("banana")
print(thislist)

Output:

['apple', 'cherry']
Copy a List:

You cannot copy a list simply by typing list2 = list1, because:

list2 will only be a reference to list1, and changes made in list1


automatically also be made in list2.

Use the copy() method

You can use the built-in List method copy() to copy a list.

Example:

Make a copy of a list with the copy() method:

thislist = ["apple", "banana", "cherry"]


mylist = thislist.copy()
print(mylist)

Output:

['apple', 'banana', 'cherry']


Tuple
 The tuple is another data type is a sequence of data similar to
a list but it is immutable(not changeable) that means data in tuple is
write protected.

 A data in tuple is return using parantheses and commands.

Tuple has five characteristic:

 Order
 Unchangeable
 immutable
 Allow duplicate value
 Allow value with multiple data types.
Ex:
a=(1,2,3,4,5)
print(a)
b=(“hello”,1,2,3,”go”)
print(b)
print(b[4])
Access Tuple Items:

You can access tuple items by referring to the index number, inside
square brackets:

Example:
Print the second item in the tuple:
thistuple = ("apple", "banana", "cherry")
print(thistuple[1])

Output:
Banana
Change Tuple Values:

 Once a tuple is created, you cannot change its values. Tuples are
unchangeable, or immutable as it also is called.

 But there is a workaround. You can convert the tuple into a


list, change the list, and convert the list back into a tuple.

Example:

Convert the tuple into a list to be able to change it:


x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)
print(x)

Output:

("apple", "kiwi", "cherry")


Python Dictionaries
 Python dictionary in an unorder sequence of data of key-value pare
form dictionary are return with in braces in form key-value.

 It is very useful to retrieve data in an optimized way among a large


amount of data.

Ex: a={1: “first_name”,2:”last name”, “age”:33}


print(a[1])
print(a[2])
print(a[“age”])

Output:
first name
last name
33
Boolean Data types:

 In python to represent Boolean value true or false. We use the boolean


Data type.

 The boolean value are used to evaluate value of expression. When the
Compare to value the expression is evaluated and python return the boolean
True or false.

Ex: x=25
y=20
z=xyz
print(z)
print(type(z))
Sets in Python

A set is an unorder collection of data items that are unique.

Python set is a collection of element that contains no duplicate element.

Python set that not maintain the order of element that is an unorder data se

Different ways creating them an adding, updating, removing, the set items.

Sets are used to store multiple items in a single variable.


Ex:

Set ={“ apple”, ” banana”, ” cherry”, ” apple”}


print(set)

Output: {‘ apple’, ’banana’, ’cherry’}

Ex:

Set ={“apple”,”banana”,”cherry”,True,1,2}
print(set)

Output: {“apple”,”banana”,”cherry”,True,2}

True is considered as the same value.


Python
Comparison
Python comparison operators are used to compare two values and return a
boolean result (True or False).

Here's an example:

Python
x = 10
y=5

print(x == y) # False
print(x != y) # True
print(x > y) # True
print(x < y) # False
print(x >= y) # True
print(x <= y) # False
THANK YOU

You might also like