0% found this document useful (0 votes)
16 views33 pages

Python Day 1

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

Python Day 1

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

PYTHON TRAINING

AGENDA
 INTRODUCTION TO PYTHON
 FUNDAMENTALS OF PYTHON
 OPERATORS AND CONDITIONS
 LOOPING CONCEPTS
• STRINGS AND FILE HANDLING
• PYTHON GUI
• FUNCTIONS
INTRODUCTION TO PYTHON

• Author of Python – Guido Van Rossum, language named after his favorite movie
• Python is an interpreted high-level general-purpose programming language.
• Easiest language than C, C++ and JAVA, so the time consumption for learning python
is less comparatively
• PRE-REQUESTIES:
 INTERPRETER-Python 3 >
 IDE-Sypder,Pycharm,jypyter note
OR
 Anaconda navigator for whole IDE
and library module
AREAS OF IMPLEMENTATION
PYTHON SCRIPTING
• Automating certain task that is done multiple times to minimize the time
and there by increase productivity.
• Other scripting languages : TCL,Shell and Perl
• Enhance scripting skills
Example:
• Automating all steps from compiling to simulation
• Automating text generation or code generation
GETTING STARTED WITH PYTHON
CONTENTS

 Variables

 String

 Lists

 Dictionary

 Tuples

 Arrays

 Operators
PYTHON
VARIABLES
Variable assignments for Integer
• Variables are the containers which are used to store values.
• The values can be integer, string, float, etc.
• To assign a value or data to a variable we use assignment operator ‘=’
• There’s no limit to how large an integer can be, which might be surprising considering that computers have a finite amount of
memory.
• Try typing the largest number you can think of into IDLE’s interactive window. Python can handle it with no problem!
• The type() function is used to find the data type of a particular variable

• Python also uses E notation to display large floating-point numbers:


Variable assignments for String
• A string is a sequence of characters.
• The string can be assigned to a variable using a single quote (‘ ’) or double quotes (“ ”).
Example:

• We can also access the character or characters of the string by indexing their position value.
• The indexing value can be in both positive as well as negative value.
Example:
• The raw string can be obtained when the ‘\n’ new line training characters are along with the string.
Example:

• Strings are immutable


LISTS
• Lists are used to store multiple items in a single variable it could be same or different datatypes.
• Lists are created using square brackets.
• Lists are mutable
• Example :
list = [3, 22, 30, 5.3, 20]
listdff = [3.4,’HI’,45]
List Slicing:
LIST METHODS
Accessing the elements of the list: Use the index/position value to access the particular element in
the list
Note : The index/position value starts from 0(Zero)

Combining two lists: Two lists can be combined together and accessed like two dimentional arrays.
• append() function inserts the element at the end of the list
• insert() function takes in two arguments one is the position and the second is the element to be inserted
• The value is inserted at the specified position inside the list.
Example:

• While using the pop() function specify the index value not the element
• extend() function is used to add multiple elements at a time at the end of the list
Example:
• While using remove function use the element itself not the index value

• List elements can be sorted in ascending order by sort() function

Exercise: How to sort in descending order

• Some of the in-built functions in list:


TUPLES
• Tuples are used to store multiple items in a single variable.
• A tuple is a collection which is ordered and unchangeable.
• Tuples are unchangeable, meaning that we cannot change, add or remove items after the tuple has been
created.
• Since tuple are indexed, tuples can have items with the same value
• Example :
tup1 = ('Robert', 'Carlos','1965','Terminator 1995', 'Actor','Florida’)

Packing and Unpacking:


x = ("Guru99", 20, "Education") # tuple packing
(company, emp, profile) = x # tuple unpacking
print(company)
print(emp)
print(profile)
Update Tuple

• 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:
thistuple = ("apple", "banana", "cherry")
y = list(thistuple)
y.append("orange")
thistuple = tuple(y)
• A comparison operator in Python can work with tuples.
• To fetch specific sets of sub-elements from tuple or list, we use this unique function called slicing
Dictionary

• Dictionary is listed in curly brackets, inside these curly brackets, keys and values are declared.
• Each key is separated from its value by a colon (:), while commas separate each element. A tuple is a
collection which is ordered and unchangeable.

Dict = {'Tim': 18,'Charlie':12,'Tiffany':22,'Robert':25}


print (Dict['Tiffany’])

Properties of Dictionary Keys


• More than one entry per key is not allowed ( no duplicate key is allowed)
• The values in the dictionary can be of any type, while the keys must be immutable like numbers, tuples, or
strings.
• Dictionary keys are case sensitive- Same key name but with the different cases are treated as different keys
in Python dictionaries.
Dictionary Methods

• The keys() method will return a list of all the keys in the dictionary.
• The values() method will return a list of all the values in the dictionary.
• The items() method will return each item in a dictionary, as tuples in a list.

• Update Dictionary: Dict.update({"Sarah":9})


• Sorting Dictionary:
• We can access the value of the dictionary using key. The get() function is also used to obtain the same result.

• del key is used to delete the particular value. We have to specify the key for it.
• In order to combine two list as dictionary we have to zip and use dict() function
ARRAYS

• Identifier: specify a name like usually, you do for variables


• Module: Python has a special module for creating arrays, called "array" – you must import it before using it
• Method: the array module has a method for initializing the array. It takes two arguments, type code, and
elements.
• Type Code: specify the data type using the type codes available (see list below)
• Elements: specify the array elements within the square brackets, for example [130,450,103]

• To access the arrays the methods are same as that of the methods used in the lists.
OPERATORS

• Operators are used to perform operations on variables and values.


• Python divides the operators in the following groups:
 Arithmetic operators
 Assignment operators
 Comparison operators
 Logical operators
 Identity operators
 Membership operators
 Bitwise operators
• Arithmetic Operators perform various arithmetic calculations like addition, subtraction, multiplication,
division, %modulus, exponent, etc.
• Assignment Operators in Python are used for assigning the value of the right operand to the left operand.
Various assignment operators used in Python are (+=, - = , *=, /= , etc.).
• Comparison Operators In Python compares the values on either side of the operand and determines the
relation between them. It is also referred to as relational operators. Various comparison operators in
python are ( ==, != , <>, >,<=, etc.)
• Logical Operators in Python are used to perform logical operations on the values of variables. The value is
either true or false.
• Bitwise operators in python act on operands as if they were strings of binary digits. They operate bit by
bit, hence the name.
Arithmetic operations with variables
• The variables can be handled with two operations namely arithmetic and logical operations.
• While dealing with operations we should be very familiar with operators and operands.
• The arithmetic operation includes addition, subtraction, multiplication and division.
Example:

• BODMAS rule is applied when more than one operation is done with the variables
Example:
Comparison operators

Exercise:
Declare two variable a and b with different value and perform all comparision operator
Logical operators Bitwise operators

Exercise:
Perform logical or operation for 1110 and 1101
Perform xor operation for 001 and 010 (^ is the xor operator)
IDENTITY OPERATOR

 In python Identity Operators are used to compare the memory location of two objects.
 The two identity operators used in Python are (is, is not).
• Operator is: It returns true if two variables point the same object and false otherwise.
• Operator is not: It returns false if two variables point the same object and true otherwise.
• Example:
x = 20
y = 20
if ( x is y ):
print("x & y SAME identity")
y=30
if ( x is not y ):
print("x & y have DIFFERENT identity")
MEMBERSHIP OPERATOR
• These operators test for membership in a sequence such as lists, strings or tuples.
• There are two membership operators that are used in Python. (in, not in).
• It gives the result based on the variable present in specified sequence or string
• Example:
x=4
y=8
list = [1, 2, 3, 4, 5 ];
if ( x in list ):
print("Line 1 - x is available in the given list")
else:
print("Line 1 - x is not available in the given list")
if ( y not in list ):
print("Line 2 - y is not available in the given list")
else:
print("Line 2 - y is available in the given list")
ASSIGNMENTS
1. Write a Python program to insert items into a list in sorted order.
Expected Output:
Original List:
[25, 45, 36, 47, 69, 48, 68, 78, 14, 36]
Sorted List:
[14, 25, 36, 36, 45, 47, 48, 68, 69, 78]
2. Write a Python program which accepts the user's first and last name and print them in reverse order with a
space between them
3. Write a Python program that assign an integer (n) and computes the value of n+nn+nnn
4. Write a Python function that takes a list of words and returns the length of the longest one.
5. Write a Python program to remove the nth index character from a list
6. Write a Python program to remove the characters which have odd index values of a given list.
7. Write a Python program to count the number of occurrence of a string in a list
Mylist=[’Dog’, ’Cat’, ‘Dog’, ‘Fish’]
8. Given an input list removes the element at index 4 and add it to the 2nd position and also, at the end of the list
Original list [34, 54, 67, 89, 11, 43, 94]
9. Find the minimum and maximum number
sampleList = [87, 45, 41, 65, 94, 41, 99, 94]
10. Below are the two lists convert it into the dictionary
keys = ['Ten', 'Twenty', 'Thirty’]
values = [10, 20, 30]
11. Write a Python program to display the current date and time.
12. Write a Python program which accepts the radius of a circle from the user and compute the circumference.
13. Write a Python program which accepts a sequence of comma-separated numbers from user and generate a
list and a tuple with those numbers.
14. Given first= ‘ Hello ‘ and last =‘ World ‘, print ‘ Hello World’ & print ‘ Hi Hello World ‘
15. Write a Python program to display the first and last colors from the following list.
color_list = [“Yellow", “Blue", "White" ,"Black"]
16. Perform following operation using exponent operator
5 power 2 + 2 power 5
ANY DOUBTS?

You might also like