Python in Easy Steps Presentation: Presented by Syed Hussain Razavi
This document provides an overview of a Python presentation covering topics like getting started with Python, performing operations, making statements, defining functions, importing modules, managing strings, and programming with objects. The presentation introduces Python, shows how to write a first program, employ variables, obtain user input, and handle errors. It also demonstrates arithmetic operations, comparing values, casting data types, writing lists, branching with if/else statements, looping with while, and breaking out of loops. Other sections explain returning values from functions, producing generators, handling exceptions, importing modules, reading/writing files, pickling data, defining classes, inheriting features through derived classes, and polymorphism.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODP, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
33 views
Python in Easy Steps Presentation: Presented by Syed Hussain Razavi
This document provides an overview of a Python presentation covering topics like getting started with Python, performing operations, making statements, defining functions, importing modules, managing strings, and programming with objects. The presentation introduces Python, shows how to write a first program, employ variables, obtain user input, and handle errors. It also demonstrates arithmetic operations, comparing values, casting data types, writing lists, branching with if/else statements, looping with while, and breaking out of loops. Other sections explain returning values from functions, producing generators, handling exceptions, importing modules, reading/writing files, pickling data, defining classes, inheriting features through derived classes, and polymorphism.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODP, PDF, TXT or read online on Scribd
You are on page 1/ 32
Python in easy steps presentation
Presented by Syed Hussain Razavi
Contents. ● Getting started ● Performing operations ● Making statement ● Defining function ● Importing modules ● Managing strings ● Programming objects Getting started ● Introducing python ● Writing your first program ● Employing variables ● Obtaining user input ● Correction error Writing your first program ● print(‘hello world’) Employing variables ● In programming, a “variable” is a container in which a data value ● can be stored within the computer’s memory. The stored value can ● then be referenced using the variable’s name ● Data to be stored in a variable is assigned in a Python program ● declaration statement with the = assignment operator. For ● example, to store the numeric value eight in a variable named “a”: ● e.g a = 8 Obtaining user input ● Just as a data value can be assigned to a variable in a Python ● script, a user-specified value can be assigned to a variable with ● the Python input() function ● Eg y = input(‘enter your number’) Correction error ● Syntax Error – occurs when the interpreter encounters code ● that does not conform to the Python language rules. For ● example, a missing quote mark around a string. T ● Runtime Error – occurs during execution of the program, at ● the time when the program runs. ● Semantic Error – occurs when the program performs ● unexpectedly. For example, when order precedence has not ● been specified in an expression Performing operation ● Doing airthmetic ● Comparing values ● Casting data types Doing airthmetic ● The operators for addition, subtraction, multiplication, and division act as you would ● expect. Care must be taken, however, to group expressions where more than one operator ● is used to clarify the expression ● print( ‘Addition:\t’ , a , ‘+’ , b , ‘=’ , a + b ) Comparing values ● Nil = 0,num = 0,max = 1,cap = ‘A’,low = ‘a’ ● print( ‘Equality :\t’ , nil , ‘==’ , num , nil == num ) ● print( ‘Equality :\t’ , cap , ‘==’ , low , cap == low ) Casting data types Although Python variables can store data of any data type it is important to recognize the ● different types of data they contain to avoid errors when manipulating that data in a ● program. There are several Python data types but by far the most common ones are str ● (string), int (integer), and float (floating-point) ● Eg int(x), float(x) Making statements ● Writing lists ● Restricting lists ● Branching with if ● Looping with While ● Breaking out of loop Writing lists ● In Python programming, a variable must be assigned an initial value (initialized) in the ● statement that declares it in a program, otherwise the interpreter will report a “not defined” error. ● Nums = [ 0 , 1 , 2 , 3 , 4 , 5 ] ● Lists can have more than one index – to represent multiple dimensions, rather than the ● single dimension of a regular list. Multi-dimensional lists of three indices and more are ● uncommon but two-dimensional lists are useful to store grid-based information such as ● X,Y coordinates. ● coords = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] Tuple ● A restrictive immutable Python list is known as a “tuple” and is created by ● assigning values as a comma-separated list between parentheses in a process known as“tuple packing”: ● colors-tuple = ( ‘Red’ , ‘Green’ , ‘Red’ , ‘Blue’, ‘Red’ ) Set ● The values in a regular list can be repeated in its elements, as in the tuple above, but a list ● of unique values can be created where duplication is not allowed. A restrictive Python list ● of unique values is known as a “set” and is created by assigning values as a comma- ● separated list between curly brackets (braces) ● phonetic-set = { ‘Alpha’ , ‘Bravo’ , ‘Charlie’ } Branching with if ● if num > 5 : ● print( ‘Number Exceeds 5’ ) ● elif num < 5 : ● print( ‘Number is Less Than 5’ ) ● else : ● print( ‘Number Is 5’ ) Looping with while ● A loop is a piece of code in a program that automatically repeats. One complete execution of all statements within a loop is called an “iteration” or a “pass”. ● i=1 ● while i < 4 : ● e on each iteration of the loop ● print( ‘\nOuter Loop Iteration:’ , i ) ● i += 1 Defining functions ● Returning values ● Producing generators ● Handling exceptions Returning values ● Like Python’s built-in str() function, which returns a string representation of the value ● specified as its argument by the caller, custom functions can also return a value to their ● caller by using the Python return keyword to specify a value to be returned. For example, ● to return to the caller the total of adding two specified argument values like this: ● def sum( a , b ) : ● return a + b ● total = sum( 8 , 4 ) ● print( ‘Eight Plus Four Is:’ , total ) Producing generators ● When a Python function is called, it executes the statements it contains and may return ● any value specified to the return keyword. After the function ends, control returns to the ● caller and the state of the function is not retained. When the function is next called, it will ● process its statements from start to finish once more. ● def incrementer() : ● i=1 ● while True : ● yield i ● i += 1 ● inc = incrementer() ● print( next( inc ) ) ● print( next( inc ) ) ● print( next( inc ) Handling exceptions ● Sections of a Python script in which it is possible to anticipate errors, such as those handling user input, can be enclosed in a try except block to handle “exception errors”. The statements to be executed are grouped in a try : block and exceptions are passed to the ensuing except : block for handling. Optionally, this may be followed by a finally : block containing statements to be executed after exceptions have been handled. ● title = ‘Python In Easy Steps’ ● try : ● print( titel ) ● except NameError as msg : ● print( msg ) Importing modules ● Def purr(pet = ‘a cat’) ● Print (pet, ‘say meow’) ● Import cat ● Cat .purr Modules ● Import math ● Num = 4 ● print( num , ‘Squared:‘ , math.pow( num , 2 ) ) Managing string ● string = ‘python in easy steps’ ● print( ‘\nCapitalized:\t’ , string.capitalize() ) ● print( ‘\nTitled:\t\t’ , string.title() ) ● print( ‘\nCentered:\t’ , string.center( 30 , ‘*’ ) ) Readind and writing files poem = 'i never sawed the dust in your eyes\n' poem += 'when i saw you so the clouds starts raining\n' file = open('poem.txt', 'w') file.write(poem) file.close() file = open('poem.txt', 'r') for line in file: print(line) file.close() file = open('poem.txt', 'a') file.write('(albert)') file.close() file = open('poem.txt', 'r') for line in file: print(line) Pickling data ● In Python, string data can easily be stored in text files using the techniques demonstrated in the previous examples. Other data types, such as numbers, lists, or dictionaries, could also be stored in text files but would require conversion to strings first. Restoring that stored data to their original data type on retrieval would require another conversion. An easier way to achieve data persistence of any data object is provided by the “pickle” module. Contd... import pickle import os if not os.path.isfile('hussain.py'): data = [0, 1] data[0] = input('enter topic') data[1] = input('enter series') file = open('hussain.py', 'wb') pickle.dump(data, file) file.close() file = open('hussain.py', 'rb') data = pickle.load(file) file.close() print('welcome back to ' + data[0] + ',' + data[1]) Programming objects ● A “class” is a specified prototype describing a set of properties that characterize an object. Each class has a data structure that can contain both functions and variables to characterize the object. ● class ClassName : ● ‘‘ class-documentation-string ‘‘‘ ● class-variable-declarations ● class-method-definitions Contd... ● class Bird : ● ‘‘’A base class to define bird properties.’’’ ● count=0 ● def __init__( self , chat ) : ● self.sound = chat ● Bird.count += 1 Inheriting features ● A Python class can be created as a brand new class, like those in previous examples, or can be “derived” from an existing class. Importantly, a derived class inherits members of the parent (base) class from which it is derived – in addition to its own members. Polymorphism ● In Python, the + character entity can be described as polymorphic because it represents either the arithmetical addition operator, in the context of numerical operands, or the string concatenation operator in the context of character operands.
(Ebook) Contemporary China : A New Superpower? by Kironska, Kristina,, Turcsanyi, Richard Q., ISBN 9781032395081, 9781032395098, 9781003350064, 9781000914986, 9781000915006, 1032395087, 1032395095, 1003350062, 1000914984 - Download the ebook now for full and detailed access