Python 2
Python 2
Comments
• Code Readability
• Explanation of the code
• Prevent execution of code
• To include resources
Pseudocode
Original code
Pseudocode
a = int(input(“Enter a number: ”))
1. Start
if a % 2 == 0:
2. Take an integer input
print(“even”)
3. Check if the number is divisible by 2
else:
4. If ‘yes’ print ‘Even’ else print ‘odd’
print(“odd”)
5. Exit
Flowchart
Graphical way of describing the steps of an
Algorithm. Start
input a
Original code
Exit
Bugs
Any mistakes or syntax errors that violate the rules of
the language are called bugs.
For example:
Syntax:
print (arg1, arg2, …., end='\n', sep=‘ ‘)
For example:
print (“Hello Python”)
Types of Function
Built-in Functions:
• Input/Output Functions: print(), input()
• Type Conversion: int(), float(), str()
• String Function: replace(), split(), upper(), substring()
• Mathematical Function: pow(), fabs(), round(), sqrt()
• File handling Functions: open(), close(), read(), write()
User-defined Functions:
• Lambda function
• Recursive Function
https://docs.python.org/3/library/functions.htm
Data Types
Use the type() function to check the data type of any variable.
Data Types
Complex Dictionary
Numbers
Standard Built-in Data Types
Numeric types
• int()
• float()
• Boolean
• complex()
Integer types
It returns the truth value i.e. either ‘True’ (1) or ‘False’ (0).
Float types
For example:
String Types
Text(s) enclosed within single (‘ ’), double (“ ”), or triple(‘‘‘ ’’’) quotes
are string.
For example:
Index
For example:
[“Welcome”, “to”, “python”, “programming”]
Length
Syntax: len(<sequence_types>)
For example:
a = “hello world”
print(len(a))
Accessing characters
Syntax: <string>[index]
For example:
a = “hello world”
print(a[2])
Slicing String
Represent the part of the original string.
Syntax: <string>[start:end:step]
For example:
greeting = "Hello, world!"
sub_string = greetings[7:12]
Replace String
Syntax: replace(<substring_to_replaced>,<substring_to_replace>)
For example:
greeting = "Hello, world!“
greetings.replace(“world”, “python”)
print(greetings)
Split String
Split the string into a list of tokens.
Syntax: split(<delimiter>)
For example:
greeting = “Welcome to the Python Programming“
print(greetings.split())
https://docs.python.org/3/library/string.html