Python Programming
Design Tools: Algorithms, Flowcharts and Pseudo-codes,
implementation of algorithms. Basics of Python Programming:
Features of Python, Writing and executing Python program,
Literal constants, variables and identifiers, Data Types, Input
operation, Comments, Reserved words, Indentation, Operators and
expressions, Expressions in Python.
Programming Design Tool
• Algorithms
• Flowcharts
• Pseudo-codes
• implementation of algorithms.
Algorithms
• Algorithm refers to a set of rules/instructions that step-by-
step define how a work is to be executed in order to get
the expected results.
Algorithm of linear search:
• Start from the leftmost element of arr[] and one by one
compare x with each element of arr[].
• If x matches with an element, return the index.
• If x doesn’t match with any of elements, return -1.
Flowcharts
• Flowcharts graphically represent the flow of a program. There are four
basic shapes used in a flow chart. Each shape has a specific use:
• oval: start / end
• parallelogram: input / output
• rectangle: calculations
• diamond: selection structures
Flowchart Example
Pseudocode
• Pseudo code is a term which is often used in programming and algorithm based
fields. It is a methodology that allows the programmer to represent the
implementation of an algorithm.
• How to write a Pseudo-code?
• Arrange the sequence of tasks and write the pseudocode accordingly.
• Start with the statement of a pseudo code which establishes the main goal or the
aim.
• Example:
• This program will allow the user to check
the number whether it's even or odd.
• Example:
• if "1"
print response
"I am case 1“
•
if "2"
print response
"I am case 2"
What is Python?
Python is a popular programming language. It was created by Guido
van Rossum, and released in 1991.
It is used for:
web development (server-side),
software development,
mathematics,
system scripting.
Features in Python
• Free and Open Source
• Easy to code
• Easy to Read
• Object-Oriented Language
• GUI Programming Support
• High-Level Language
• Easy to Debug
• Python is a Portable language
• Dynamically Typed Language
• Allocating Memory Dynamically
Python Syntax compared to other
programming languages
Python was designed for readability, and has some similarities to the
English language with influence from mathematics.
Python uses new lines to complete a command, as opposed to other
programming languages which often use semicolons or parentheses.
Python relies on indentation, using whitespace, to define scope; such as
the scope of loops, functions and classes. Other programming languages
often use curly-brackets for this purpose.
For Example:
print("Hello, World!")
Python Install
To check if you have python installed on a Windows PC, search
in the start bar for Python or run the following on the Command
Line (cmd.exe):
C:UsersYour Name>python –version
The way to run a python file is like this on the command line:
C:UsersYour Name>python helloworld.py
Program save as : helloworld.py
print("Hello, World!")
Python Program
Python Indentation
Indentation refers to the spaces at the beginning of a code
line.
Where in other programming languages the indentation in
code is for readability only, the indentation in Python is very
important.
Python uses indentation to indicate a block of code.
For Ex.
if 5 > 2:
print("Five is greater than two!")
if 5 > 2:
print("Five is greater than two!")
Python Keywords
• Python has a set of keywords that are reserved words that
cannot be used as variable names, function names, or any other
identifiers:
• Keyword Description
• And A logical operator
• As To create an alias
• Assert For debugging
• Break To break out of a loop
• Class To define a class
• Continue To continue to the next iteration of a loop
• Def To define a function
Comments
Python has commenting capability for the purpose of in-code documentation.
Comments start with a #, and Python will render the rest of the line as a comment:
Example
1.Comments in Python:
#This is a comment.
print("Hello, World!")
2.Multiline Comments
To add a multiline comment you could insert a # for each line:
Example
#This is a comment
#written in
#more than just one line
print("Hello, World!")
Python Variables
Variables
• Variables are containers for storing data values.
• Creating Variables
• Python has no command for declaring a variable.
• A variable is created the moment you first assign a
value to it.
Example:
x = 5
y = "John"
print(x)
print(y)
Casting
If you want to specify the data type of a variable, this can be
done with casting.
Example:
x = str(3) # x will be '3'
y = int(3) # y will be 3
z = float(3) # z will be 3.0
Type()
Get the Type
You can get the data type of a variable with
the type() function.
Example:
x = 5
y = "John"
print(type(x))
print(type(y))
Single or Double Quotes?
• String variables can be declared either by using single or double quotes:
Example
x = "John"
# is the same as
x = 'John‘
Case-Sensitive
• Variable names are case-sensitive.
Example
• This will create two variables:
a = 4
A = "Sally"
#A will not overwrite a
• Example
Illegal variable names:
2myvar = "John"
my-var = "John"
my var = "John"
Legal variable names:
• myvar = "John"
my_var = "John"
_my_var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"
Many Values to Multiple Variables
• Python allows you to assign values to multiple variables in one
line:
Example:
• x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)
One Value to Multiple Variables
• And you can assign the same value to multiple variables in one line:
Example
• x = y = z = "Orange"
print(x)
print(y)
print(z)
Unpack a Collection
If you have a collection of values in a list, tuple etc. Python
allows you to extract the values into variables. This is
called unpacking.
Example
Unpack a list:
fruits = ["apple", "banana", "cherry"]
x, y, z = fruits
print(x)
print(y)
print(z)
Global Variables
• Variables that are created outside of a function (as in all of the
examples above) are known as global variables.
• Global variables can be used by everyone, both inside of
functions and outside.
• Example
• Create a variable outside of a function, and use it inside the
function
• x = "awesome"
def myfunc():
print("Python is " + x)
myfunc()
• Create a variable inside a function, with the same name as
the global variable
• x = "awesome"
def myfunc():
x = "fantastic"
print("Python is " + x)
myfunc()
print("Python is " + x)
• If you use the global keyword, the variable belongs to the
global scope:
• def myfunc():
global x
x = "fantastic"
myfunc()
print("Python is " + x)
Python Data Types
Built-in Data Types
• In programming, data type is an important concept.
• Variables can store data of different types, and different types can do
different things.
• Python has the following data types built-in by default, in these categories:
• Text Type:str
• Numeric Types:int, float, complex
• Sequence Types:list, tuple, range
• Mapping Type:dict
• Set Types:set, frozenset
• Boolean Type:bool
• Binary Types:bytes, bytearray, memoryview
• None Type:NoneType
Examples
Example Data Type
• x = "Hello World “ str
• x = 20 int
• x = 20.5 float
• x = 1j complex
• x = ["apple", "banana", "cherry"] list
• x = ("apple", "banana", "cherry") tuple
• x = range(6) range
• x = {"name" : "John", "age" : 36} dict
• x = {"apple", "banana", "cherry"} set
• x = True bool
• x = b"Hello“ bytes
Random Number
• Python does not have a random() function to make a random
number, but Python has a built-in module called random that
can be used to make random numbers:
• Example
Import the random module, and display a random number
between 1 and 9:
import random
print(random.randrange(1, 10))
Python Operators
• Operators are used to perform operations on variables and
values.
• In the example below, we use the + operator to add together
two values:
• Example
print(10 + 5)
Python divides the operators in the following groups:
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Identity operators
• Membership operators
• Bitwise operators
Python Arithmetic Operators
Operator Name Example
+ Addition x + y
- Subtraction x – y
* Multiplication x * y
/ Division x / y
% Modulus x % y
** Exponentiation x ** y
// Floor division x // y
Python Assignment Operators
Operator Example Same As
= x = 5 x = 5
+= x += 3 x = x + 3
-= x -= 3 x = x – 3
*= x *= 3 x = x * 3
/= x /= 3 x = x / 3
%= x %= 3 x = x % 3
//= x //= 3 x = x // 3
**= x **= 3 x = x ** 3
Python Comparison Operators
Operator Name Example
== Equal x == y
!= Not equal x != y
> Greater than x > y
< Less than x < y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
Python Logical Operators
Operator Description Example
and Returns True if both statements are true x < 5 and x<10
or Returns True if one of the statements is true x < 5 or x < 4
not Reverse the result, returns False if the result is true not(x < 5 and x < 10)
Python Identity Operators
• Operator Description Example
is Returns True if both variables are the same object x is y
is not Returns True if both variables are not the same object x is not y
Python Membership Operators
Operator Description Example
in Returns True if a sequence with the specified
value is present in the object x in y
not in Returns True if a sequence with the specified
value is not present in the object x not in y
Python Bitwise Operators
Operator Name Description Example
• & AND Sets each bit to 1 if both bits are 1 x & y
• | OR Sets each bit to 1 if one of two bits is 1 x | y
• ^ XOR Sets each bit to 1 if only one of two bits is 1 x ^y
• ~ NOT Inverts all the bits ~x
• << Zero fill Shift left by pushing
• left shift zeros in from the right and let the leftmost
• bits fall off x << 2
• >> Signed right shift Shift right by pushing copies
• of the leftmost bit in from the left,
• and let the rightmost bits fall off x >> 2

Basic concept of Python.pptx includes design tool, identifier, variables.

  • 1.
    Python Programming Design Tools:Algorithms, Flowcharts and Pseudo-codes, implementation of algorithms. Basics of Python Programming: Features of Python, Writing and executing Python program, Literal constants, variables and identifiers, Data Types, Input operation, Comments, Reserved words, Indentation, Operators and expressions, Expressions in Python.
  • 2.
    Programming Design Tool •Algorithms • Flowcharts • Pseudo-codes • implementation of algorithms.
  • 3.
    Algorithms • Algorithm refersto a set of rules/instructions that step-by- step define how a work is to be executed in order to get the expected results. Algorithm of linear search: • Start from the leftmost element of arr[] and one by one compare x with each element of arr[]. • If x matches with an element, return the index. • If x doesn’t match with any of elements, return -1.
  • 4.
    Flowcharts • Flowcharts graphicallyrepresent the flow of a program. There are four basic shapes used in a flow chart. Each shape has a specific use: • oval: start / end • parallelogram: input / output • rectangle: calculations • diamond: selection structures
  • 5.
  • 6.
    Pseudocode • Pseudo codeis a term which is often used in programming and algorithm based fields. It is a methodology that allows the programmer to represent the implementation of an algorithm. • How to write a Pseudo-code? • Arrange the sequence of tasks and write the pseudocode accordingly. • Start with the statement of a pseudo code which establishes the main goal or the aim. • Example: • This program will allow the user to check the number whether it's even or odd. • Example: • if "1" print response "I am case 1“ • if "2" print response "I am case 2"
  • 7.
    What is Python? Pythonis a popular programming language. It was created by Guido van Rossum, and released in 1991. It is used for: web development (server-side), software development, mathematics, system scripting.
  • 8.
    Features in Python •Free and Open Source • Easy to code • Easy to Read • Object-Oriented Language • GUI Programming Support • High-Level Language • Easy to Debug • Python is a Portable language • Dynamically Typed Language • Allocating Memory Dynamically
  • 9.
    Python Syntax comparedto other programming languages Python was designed for readability, and has some similarities to the English language with influence from mathematics. Python uses new lines to complete a command, as opposed to other programming languages which often use semicolons or parentheses. Python relies on indentation, using whitespace, to define scope; such as the scope of loops, functions and classes. Other programming languages often use curly-brackets for this purpose. For Example: print("Hello, World!")
  • 10.
    Python Install To checkif you have python installed on a Windows PC, search in the start bar for Python or run the following on the Command Line (cmd.exe): C:UsersYour Name>python –version The way to run a python file is like this on the command line: C:UsersYour Name>python helloworld.py Program save as : helloworld.py print("Hello, World!")
  • 11.
    Python Program Python Indentation Indentationrefers to the spaces at the beginning of a code line. Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important. Python uses indentation to indicate a block of code. For Ex. if 5 > 2: print("Five is greater than two!") if 5 > 2: print("Five is greater than two!")
  • 12.
    Python Keywords • Pythonhas a set of keywords that are reserved words that cannot be used as variable names, function names, or any other identifiers: • Keyword Description • And A logical operator • As To create an alias • Assert For debugging • Break To break out of a loop • Class To define a class • Continue To continue to the next iteration of a loop • Def To define a function
  • 13.
    Comments Python has commentingcapability for the purpose of in-code documentation. Comments start with a #, and Python will render the rest of the line as a comment: Example 1.Comments in Python: #This is a comment. print("Hello, World!") 2.Multiline Comments To add a multiline comment you could insert a # for each line: Example #This is a comment #written in #more than just one line print("Hello, World!")
  • 14.
    Python Variables Variables • Variablesare containers for storing data values. • Creating Variables • Python has no command for declaring a variable. • A variable is created the moment you first assign a value to it. Example: x = 5 y = "John" print(x) print(y)
  • 15.
    Casting If you wantto specify the data type of a variable, this can be done with casting. Example: x = str(3) # x will be '3' y = int(3) # y will be 3 z = float(3) # z will be 3.0
  • 16.
    Type() Get the Type Youcan get the data type of a variable with the type() function. Example: x = 5 y = "John" print(type(x)) print(type(y))
  • 17.
    Single or DoubleQuotes? • String variables can be declared either by using single or double quotes: Example x = "John" # is the same as x = 'John‘ Case-Sensitive • Variable names are case-sensitive. Example • This will create two variables: a = 4 A = "Sally" #A will not overwrite a
  • 18.
    • Example Illegal variablenames: 2myvar = "John" my-var = "John" my var = "John"
  • 19.
    Legal variable names: •myvar = "John" my_var = "John" _my_var = "John" myVar = "John" MYVAR = "John" myvar2 = "John"
  • 20.
    Many Values toMultiple Variables • Python allows you to assign values to multiple variables in one line: Example: • x, y, z = "Orange", "Banana", "Cherry" print(x) print(y) print(z)
  • 21.
    One Value toMultiple Variables • And you can assign the same value to multiple variables in one line: Example • x = y = z = "Orange" print(x) print(y) print(z)
  • 22.
    Unpack a Collection Ifyou have a collection of values in a list, tuple etc. Python allows you to extract the values into variables. This is called unpacking. Example Unpack a list: fruits = ["apple", "banana", "cherry"] x, y, z = fruits print(x) print(y) print(z)
  • 23.
    Global Variables • Variablesthat are created outside of a function (as in all of the examples above) are known as global variables. • Global variables can be used by everyone, both inside of functions and outside. • Example • Create a variable outside of a function, and use it inside the function • x = "awesome" def myfunc(): print("Python is " + x) myfunc()
  • 24.
    • Create avariable inside a function, with the same name as the global variable • x = "awesome" def myfunc(): x = "fantastic" print("Python is " + x) myfunc() print("Python is " + x)
  • 25.
    • If youuse the global keyword, the variable belongs to the global scope: • def myfunc(): global x x = "fantastic" myfunc() print("Python is " + x)
  • 26.
    Python Data Types Built-inData Types • In programming, data type is an important concept. • Variables can store data of different types, and different types can do different things. • Python has the following data types built-in by default, in these categories: • Text Type:str • Numeric Types:int, float, complex • Sequence Types:list, tuple, range • Mapping Type:dict • Set Types:set, frozenset • Boolean Type:bool • Binary Types:bytes, bytearray, memoryview • None Type:NoneType
  • 27.
    Examples Example Data Type •x = "Hello World “ str • x = 20 int • x = 20.5 float • x = 1j complex • x = ["apple", "banana", "cherry"] list • x = ("apple", "banana", "cherry") tuple • x = range(6) range • x = {"name" : "John", "age" : 36} dict • x = {"apple", "banana", "cherry"} set • x = True bool • x = b"Hello“ bytes
  • 28.
    Random Number • Pythondoes not have a random() function to make a random number, but Python has a built-in module called random that can be used to make random numbers: • Example Import the random module, and display a random number between 1 and 9: import random print(random.randrange(1, 10))
  • 29.
    Python Operators • Operatorsare used to perform operations on variables and values. • In the example below, we use the + operator to add together two values: • Example print(10 + 5)
  • 30.
    Python divides theoperators in the following groups: • Arithmetic operators • Assignment operators • Comparison operators • Logical operators • Identity operators • Membership operators • Bitwise operators
  • 31.
    Python Arithmetic Operators OperatorName Example + Addition x + y - Subtraction x – y * Multiplication x * y / Division x / y % Modulus x % y ** Exponentiation x ** y // Floor division x // y
  • 32.
    Python Assignment Operators OperatorExample Same As = x = 5 x = 5 += x += 3 x = x + 3 -= x -= 3 x = x – 3 *= x *= 3 x = x * 3 /= x /= 3 x = x / 3 %= x %= 3 x = x % 3 //= x //= 3 x = x // 3 **= x **= 3 x = x ** 3
  • 33.
    Python Comparison Operators OperatorName Example == Equal x == y != Not equal x != y > Greater than x > y < Less than x < y >= Greater than or equal to x >= y <= Less than or equal to x <= y
  • 34.
    Python Logical Operators OperatorDescription Example and Returns True if both statements are true x < 5 and x<10 or Returns True if one of the statements is true x < 5 or x < 4 not Reverse the result, returns False if the result is true not(x < 5 and x < 10)
  • 35.
    Python Identity Operators •Operator Description Example is Returns True if both variables are the same object x is y is not Returns True if both variables are not the same object x is not y
  • 36.
    Python Membership Operators OperatorDescription Example in Returns True if a sequence with the specified value is present in the object x in y not in Returns True if a sequence with the specified value is not present in the object x not in y
  • 37.
    Python Bitwise Operators OperatorName Description Example • & AND Sets each bit to 1 if both bits are 1 x & y • | OR Sets each bit to 1 if one of two bits is 1 x | y • ^ XOR Sets each bit to 1 if only one of two bits is 1 x ^y • ~ NOT Inverts all the bits ~x • << Zero fill Shift left by pushing • left shift zeros in from the right and let the leftmost • bits fall off x << 2 • >> Signed right shift Shift right by pushing copies • of the leftmost bit in from the left, • and let the rightmost bits fall off x >> 2