832 likes | 1.24k Views
This Edureka Python tutorial will help you in understanding the various fundamentals of Python programming with examples in detail. This Python tutorial helps you to learn following topics: <br>1. Introduction to Python <br>2. Who uses Python <br>3. Features of Python <br>4. Operators in Python <br>5. Datatypes in Python <br>6. Flow Control <br>7. Functions in Python <br>8. File Handling in Python
E N D
EDUREKA PYTHON CERTIFICATION TRAINING What is Hadoop? www.edureka.co/python
Agenda ➢ Python Introduction ➢ Who uses Python? ➢ Python Features ➢ Operators in Python ➢ Datatypes in Python ➢ Flow Control ➢ Functions in Python ➢ File Handling in Python www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Python Introduction www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Python Introduction ➢ Python is an interpreted, object-oriented, high-level programming language with dynamic semantics ➢ Python was created by Guido Rossum in 1989 and is very easy to learn Object Oriented Procedure Oriented High Level Language Easy to Learn www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Who uses Python? www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Who Uses Python? Dropbox storage service codes both its server and desktop client software primarily in Python. The Raspberry Pi single- board computer promotes Python as its educational language. The popular YouTube video sharing service is largely written in Python. Google makes extensive use of Python in its web search systems. C O M PA N I E S U S I N G P Y T H O N NASA, Los Alamos, Fermilab, JPL, and others use Python for scientific programming tasks. Netflix and Yelp have both documented the role of Python in their software infrastructures. BitTorrent peer-to-peer file sharing system began its life as a Python program. The NSA uses Python for cryptography and intelligence analysis. www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Python Features www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Python Features Simple and Easy to Learn Python is a simple and easy to learn, read & write Free and Open Source Python is an example of a FLOSS (Free/Libre and Open Source Software) which means one can freely distribute copies of this software, read it's source code, modify it, etc. www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Python Features High-level Language a=3 b=5 sum=a+b print(sum) 01001010110110 11101100001101 10110101100101 0101011010 compile One does not need to bother about the low-level details like memory allocation, etc. while writing Python script Portable Supported by many platforms like Linux, Windows, FreeBSD, Macintosh, Solaris, OS/2, Amiga, AROS, AS/400, BeOS, OS/390, PlayStation, Windows CE, etc. www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Python Features Procedure Oriented Supports different Programming Paradigm Object Oriented Python supports procedure-oriented programming as well as object-oriented programming. C/C++ Extensible Python code can invoke C and C++ libraries, can be called from and C++ programs, can integrate with Java and .NET components www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Zen of Python C/C++ www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Installation Steps www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Python Installation Go to www.python.org 1 Under Downloads tab, select the latest Python version for Windows 2 www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Python Installation Open the installer and click on Run 3 www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Python Installation Click on Install Now 5 Select ‘Add Python 3.6 to PATH’ 4 www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Python Installation Start IDLE which is a Python GUI & start scripting 6 www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Python Installation C/C++ IDLE stands for integrated Development and Learning Environment www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Operators in Python www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Operators in Python 1 Arithmetic Operators 2 Assignment Operators 3 Comparison Operators 4 Logical Operators 5 Bitwise Operators 6 Identity Operators 7 Special Operators www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Arithmetic Operators + >> 2 + 3 5 >> +2 1 Arithmetic Operators Add two operands or unary plus 2 Assignment Operators - >> 3 – 1 2 >> -2 3 Comparison Operators Subtract two operands or unary subtract 4 Logical Operators * >> 2 * 3 6 Multiply two operands 5 Bitwise Operators 6 Identity Operators / Divide left operand with the right and result is in float >> 6 / 3 2.0 7 Special Operators www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Arithmetic Operators 1 Arithmetic Operators ** >> 2 + 3 5 >> +2 Left operand raised to the power of right 2 Assignment Operators 3 Comparison Operators % >> 3 – 1 2 >> -2 Remainder of the division of left operand by the right 4 Logical Operators 5 Bitwise Operators // division that results into whole number adjusted to the left in the number line >> 2 * 3 6 6 Identity Operators 7 Special Operators www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Assignment Operators www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Assignment Operators >> x = 5 /= 1 Arithmetic Operators >> x/=5 >> print(x) 1.0 x = x / <right operand> 2 Assignment Operators %= >> x%=5 >> print(x) 0 3 Comparison Operators x = x % <right operand> 4 Logical Operators //= >> x //= 2 >> print(x) 2 x = x // <right operand> 5 Bitwise Operators 6 Identity Operators **= >> x**= 5 >> print(x) 32 x = x ** <right operand> 7 Special Operators www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Assignment Operators >> x = 5 &= 1 Arithmetic Operators >> x&=2 >> print(x) 0 x = x & <right operand> 2 Assignment Operators |= >> x|=2 >> print(x) >> 7 3 Comparison Operators x = x | <right operand> 4 Logical Operators ^= >> x ^= 2 >> print(x) 7 x = x ^ <right operand> 5 Bitwise Operators 6 Identity Operators >>= >> x >>= 2 >> print(x) 1 x = x >> <right operand> 7 Special Operators www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Comparison Operators www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Comparison Operators > 1 Arithmetic Operators >> 2 > 3 False True if left operand is greater than the right 2 Assignment Operators < 3 Comparison Operators >> 2 < 3 True True if left operand is less than the right 4 Logical Operators == >>2 == 2 True True if left operand is equal to right 5 Bitwise Operators 6 Identity Operators != >> x >>= 2 >>print(x) 1 True if left operand is not equal to the right 7 Special Operators www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Logical Operators www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Logical Operators 1 Arithmetic Operators and 2 Assignment Operators >> 2 and 3 3 Returns x if x is False , y otherwise 3 Comparison Operators or >> 2 or 3 2 Returns y if x is False, x otherwise 4 Logical Operators not 5 Bitwise Operators >> not 1 False Returns True if x is True, False otherwise 6 Identity Operators 7 Special Operators www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Bitwise Operators www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Bitwise Operators 1 Arithmetic Operators a | b 111 7 101 5 111 7 2 Assignment Operators Perform OR operation on each bit of the no. 3 Comparison Operators a & b 111 7 101 5 111 5 Perform AND operation on each bit of the number 4 Logical Operators 5 Bitwise Operators a ^ b 111 7 101 5 111 2 Perform XOR operation on each bit of the number 6 Identity Operators 7 Membership Operators www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Bitwise Operators 1 Arithmetic Operators 2 Assignment Operators a >> b 3 >> 2 = 0 0011 0000 Shift a right by b bits 3 Comparison Operators 4 Logical Operators a << b 3 << 2 = 12 0011 1100 Shift a left by b bits 5 Bitwise Operators 6 Identity Operators 7 Membership Operators www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Identity Operators www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Identity Operators 1 Arithmetic Operators 2 Assignment Operators is >> x = 5 >> x is 5 True True if the operands are identical (refer to the same object) 3 Comparison Operators 4 Logical Operators is not >> x = 5 >> x is not 5 False True if the operands are not identical (do not refer to the same object) 5 Bitwise Operators 6 Identity Operators 7 Membership Operators www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Membership Operators www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Membership Operators 1 Arithmetic Operators X = [1, 2, 3, 4, 5] 2 Assignment Operators is >> 3 in x True True if the operands are identical (refer to the same object) 3 Comparison Operators 4 Logical Operators is not True if the operands are not identical (do not refer to the same object) >> 3 not in x False 5 Bitwise Operators 6 Identity Operators 7 Membership Operators www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Datatypes www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Datatype ➢ No need to declare variables before using them ➢ Python is a loosely typed language. Therefore, no need to define the datatype of variables C/C++ Datatype Immutable Mutable Numbers Lists Strings Tuples Dictionaries Sets www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Datatype Numbers Strings Immutable Tuples Lists Dictionaries Mutable Sets www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Datatype ➢ Strings are sequences of one-character strings Example: sample = ‘Welcome to Python Tutorial’ Numbers Strings Immutable or sample = “Welcome to Python Tutorial” Tuples ➢ Multi-line strings can be denoted using triple quotes, ''' or ""“ Example: sample = “””Don’t Go Gentle into the good Night Rage! Rage, against the dying light””” Lists Dictionaries Mutable Sets www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Datatype Sequence Operations: ➢ Concatenation: Numbers ‘Tutorial’ ‘Python’ ‘Edureka Tutorial’ Strings Immutable ➢ Repetition Tuples ** 2 ‘Edureka’ ‘EdurekaEdureka’ Lists ➢ Slicing Dictionaries string1[2:7] string1 = ‘Edureka’ Mutable ‘ureka’ ➢ Indexing Sets string1[-1] + string[1] string1 = ‘Edureka’ ‘da’ www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Datatype Type Specific Method: ➢ find(): Numbers str.find ( ‘ureka’ ) str = ‘Edureka’ ‘ureka’ Strings Immutable ➢ replace() Tuples ‘Eureka’ str.replace ( ‘Ed’,’E’ ) str = ‘Edureka’ Lists ➢ split() [‘E’, ‘d’, ‘u’, ‘r’, ‘e’, ‘k’, ‘a’] Dictionaries s.split ( ‘,’ ) str = ‘E, d, u, r, e, k, a’ Mutable ➢ count() Sets str = ‘Edureka’ 2 str.count(‘e’, beg=2, end=6) www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Datatype Type Specific Method: ➢ upper(): Numbers str.upper () str = ‘edureka’ ‘EDUREKA’ Strings Immutable ➢ max() Tuples ‘u’ max (str) str = ‘Edureka’ Lists ➢ min() str = ‘Edureka’ Dictionaries min ( str ) ‘a’ Mutable Sets ➢ isalpha() str = ‘Edureka’ str.isalpha() True www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Datatype Numbers ➢ A tuple is a sequence of immutable Python objects like floating number, string literals, etc. ➢ The tuples can’t be changed unlike lists ➢ Tuples are defined using curve braces Strings Immutable Tuples Lists Dictionaries Mutable myTuple = ( ‘Edureka’ , 2.4, 5, ‘Python’ ) Sets www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Datatype Sequence Operations: ➢ Concatenation: Numbers tup +( ‘d’ ) tup = ( ‘a’ , ‘b’ , ‘c’ ) ( ‘a’ , ‘b’ , ‘c’ , ‘d’ ) Strings Immutable ➢ Repetition Tuples (‘a’ , ‘b’ , ‘c’ , ‘a’ , ‘b’ , ‘c’ ) tup * 2 tup = ( ‘a’ , ‘b’ , ‘c’ ) Lists ➢ Slicing Dictionaries tup[1:2] tup = ( ‘a’ , ‘b’ , ‘c’ ) Mutable ( ‘b’ , ‘c’ ) ➢ Indexing Sets tup[0] tup = ( ‘a’ , ‘b’ , ‘c’ ) ‘a’ www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Datatype Numbers ➢ A list is a sequence of mutable Python objects like floating number, string literals, etc. ➢ The lists can be modified ➢ Tuples are defined using square braces Strings Immutable Tuples Lists Dictionaries Mutable myList = [ ‘Edureka’ , 2.4, 5, ‘Python’ ] Sets www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Datatype Sequence Operations: ➢ Concatenation: Numbers ‘d’ [ ‘1’ , ‘b’ , 2.5 ] [ 1 , ‘b’ , 2.5 , ‘d’ ] Strings Immutable ➢ Repetition Tuples ** 2 [ ‘a’ , ‘b’ , 2.5 ] [‘a’ , ‘b’ , ‘a’ , ‘b’ ] Lists ➢ Slicing Dictionaries list[1:3] list = [‘a’ , ‘b’ , ‘c’ ,’d’] Mutable ( ‘b’ , ‘c’, ‘d’ ) ➢ Indexing Sets ‘a’ list[0] list = [‘a’ , ‘b’ , ‘c’] www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Datatype Type Specific Method ➢ append(value) Numbers List.append (‘d’ ) list = [1, ‘a’, 2.5] [ 1 , ‘a’ , 2.5 , ‘d’ ] Strings Immutable ➢ extend(list) Tuples list.extend ( [‘c’, ‘d’] ) list = [1, ‘a’, 2.5] [1 , ‘a’ , 2.5 , ‘c’, ‘d’] Lists ➢ insert(index, value) Dictionaries List.insert(2,’b’) list = [1, ‘a’, 2.5] Mutable [1, ’a’, ‘b’ , 2.5 ] ➢ pop() Sets [ ‘a’, ‘b’ ] List.pop() list = [‘a’ , ‘b’ , ‘c’] www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Datatype Numbers ➢ Dictionaries are perhaps the most flexible built-in data type in Python ➢ Dictionaries, items are stored and fetched by key, instead of by positional offset Strings Immutable Tuples Value Lists Dictionaries Mutable myDict = { 1: ‘Josh’ , 2: ‘Bob’, 3: ‘James’ } Sets Key www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Datatype Dictionary Examples ➢ empty dictionary Numbers myDict = {} Strings Immutable ➢ dictionary with integer keys Tuples myDict = {1: 'apple', 2: 'ball'} Lists ➢ dictionary with mixed keys Dictionaries Mutable myDict = {'name': 'John', 1: [2, 4, 3]} ➢ from sequence having each item as a pair Sets myDict = dict([(1,'apple'), (2,'ball')]) www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Datatype Dictionary Methods ➢ Accessing Dictionary Numbers myDict [1] myDict = {1: 'apple', 2: 'ball'} ‘apple’ Strings Immutable ➢ len() Tuples len(myDict) myDict = {1: 'apple', 2: 'ball'} 2 Lists ➢ key() myDict.key() Dictionaries [1, 2] myDict = {1: 'apple', 2: 'ball'} Mutable ➢ values() Sets myDict.values() [‘apple’, ‘ball’] myDict = {1: 'apple', 2: 'ball'} www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING