Phython Reviewer Python
Phython Reviewer Python
Phython Reviewer Python
PYTHON
o is a general-purpose interpreted, interactive, object-oriented, and high-level programming
language. It was created by Guido van Rossum during 1985- 1990. Like Perl, Python source code is also
available under the GNU General Public License (GPL). This tutorial gives enough understanding on
Python programming language.
o is a high-level, interpreted, interactive and object-oriented scripting language. Python is
designed to be highly readable. It uses English keywords frequently where as other languages use
punctuation, and it has fewer syntactical constructions than other languages
o is a MUST for students and working professionals to become a great Software Engineer specially
when they are working in Web Development Domain.
FEATURES OF PYTHON:
• Easy-to-learn − Python has few keywords, simple structure, and a clearly defined syntax. This allows
the student to pick up the language quickly.
• Easy-to-read − Python code is more clearly defined and visible to the eyes.
• Easy-to-maintain − Python's source code is fairly easy-to-maintain.
• A broad standard library − Python's bulk of the library is very portable and cross-platform compatible
on UNIX, Windows, and Macintosh.
• Interactive Mode − Python has support for an interactive mode which allows interactive testing and
debugging of snippets of code.
• Portable − Python can run on a wide variety of hardware platforms and has the same interface on all
platforms.
• Extendable − You can add low-level modules to the Python interpreter. These modules enable
programmers to add to or customize their tools to be more efficient.
• Databases − Python provides interfaces to all major commercial databases.
• GUI Programming − Python supports GUI applications that can be created and ported to many system
calls, libraries and windows systems, such as Windows MFC, Macintosh, and the X Window system of
Unix.
• Scalable − Python provides a better structure and support for large programs than shell scripting.
HISTORY OF PYTHON
Python was developed by Guido van Rossum in the late eighties and early nineties at the National
Research Institute for Mathematics and Computer Science in the Netherlands.
Python is derived from many other languages, including ABC, Modula-3, C, C++, Algol-68, SmallTalk, and
Unix shell and other scripting languages.
Python is copyrighted. Like Perl, Python source code is now available under the GNU General Public
License (GPL).
Python is now maintained by a core development team at the institute, although Guido van Rossum still
holds a vital role in directing its progress. Python is available on a wide variety of platforms including
Linux and Mac OS X. Let's understand how to set up our Python environment.
IDENTIFIERS
A Python identifier is a name used to identify a variable, function, class, module or other object. An
identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters,
underscores and digits (0 to 9).
Python does not allow punctuation characters such as @, $, and % within identifiers. Python is a case
sensitive programming language. Thus, Manpower and manpower are two different identifiers in
Python.
Naming conventions of Python identifiers:
• Class names start with an uppercase letter. All other identifiers start with a lowercase letter.
• Starting an identifier with a single leading underscore indicates that the identifier is private.
• Starting an identifier with two leading underscores indicates a strongly private identifier.
• If the identifier also ends with two trailing underscores, the identifier is a language-defined special
name.
Lines and Indentation
o Python provides no braces to indicate blocks of code for class and function definitions or flow
control. Blocks of code are denoted by line indentation, which is rigidly enforced.
o The number of spaces in the indentation is variable, but all statements within the block must be
indented the same amount.
Multi-Line Statements
o Statements in Python typically end with a new line. Python does, however, allow the use of the
line continuation character (\) to denote that the line should continue. Statements contained within
the [], {}, or () brackets do not need to use the line continuation character.
Quotation
o Python accepts single ('), double (") and triple (''' or """) quotes to denote string literals, as long
as the same type of quote starts and ends the string. The triple quotes are used to span the string
across multiple lines.
Comments
o A hash sign (#) that is not inside a string literal begins a comment. All characters after the # and
up to the end of the physical line are part of the comment and the Python interpreter ignores them.
Reserved Words
The following list shows the Python keywords. These are reserved words and you cannot use them as
constant or variable or any other identifier names. All the Python keywords contain lowercase letters
only.
STANDARD DATA TYPES
The data stored in memory can be of many types. For example, a person's age is stored as a numeric
value and his or her address is stored as alphanumeric characters. Python has various standard data
types that are used to define the operations possible on them and the storage method for each of them.
A. Numbers
Number data types store numeric values. They are immutable data types, means that changing the
value of a number data type results in a newly allocated object.
Python supports four different numerical types:
int (signed integers) − They are often called just integers or ints, are positive or negative whole
numbers with no decimal point.
long (long integers) − Also called longs, they are integers of unlimited size, written like integers
and followed by an uppercase or lowercase L.
float (floating point real values) − Also called floats, they represent real numbers and are
written with a decimal point dividing the integer and fractional parts. Floats may also be in
scientific notation, with E or e indicating the power of 10 (2.5e2 = 2.5 x 102 = 250).
complex (complex numbers) − are of the form a + bJ, where a and b are floats and J (or j)
represents the square root of -1 (which is an imaginary number). The real part of the number is
a, and the imaginary part is b. Complex numbers are not used much in Python programming.
B. Strings
Strings in Python are identified as a contiguous set of characters represented in the quotation
marks. Python allows for either pairs of single or double quotes. Subsets of strings can be taken
using the slice operator ([ ] and [:]) with indexes starting at 0 in the beginning of the string and
working their way from -1 at the end.
The plus (+) sign is the string concatenation operator and the asterisk (*) is the repetition
operator.
DECISION MAKING
Decision making is anticipation of conditions occurring while execution of the program and
specifying actions taken according to the conditions.
Decision structures evaluate multiple expressions which produce TRUE or FALSE as outcome.
You need to determine which action to take and which statements to execute if outcome is TRUE or
FALSE otherwise.
SAMPLE CODES:
PROBLEM: Input a number. Determine if the number is odd or even
OUTPUT:
Enter a number 2
Number is even
SOURCE CODE:
x=int(input('Enter a number'))
if x%2==0:
print('number is even')
print("this is also part of the true statement")
print("true pa dih")
else:
print('the number is odd')
print("end of program")
PROBLEM: Coin problem. Input an amount, output the amount broken down into:
OUTPUT:
Enter an amount 1
1000 0
500 0
200 0
100 0
50 0
20 0
10 0
50
11
SOURCE CODE:
x=int(input('Enter an amount'))
p1= int(x/1000)
p2= int(x%1000/500)
p3= int(x%1000%500/200)
p4=int (x%1000%500%200/100)
p5 = int(x%1000%500%200%100/50)
p6= int(x%1000%500%200%100%50/20)
p7 = int (x%1000%500%200%100%50%20/10)
p8=int(x%1000%500%200%100%50%20%10/5)
p9= int (x%1000%500%200%100%50%20%10%5/1)
print('1000', p1)
print('500', p2)
print('200', p3)
print('100', p4)
print('50', p5)
print('20', p6)
print('10', p7)
print('5', p8)
print('1', p9)
PROBLEM: An employee is paid based on the no. of hours worked and rate per hour. His tax rate is
dependent on his salary grade. Write a program that accepts the number of hours an
employee worked and the rate per hour and salary grade then output the total pay the
student receives
PROCESS:
Compute the total pay
Tp= hours_worked * rate_per_hour
SOURCE CODE:
OUTPUT:
Total pay
PROBLEM: Input score and number and items, output the transmuted and equivalent
EQUIVALENT:
Tg eq
97-100 1.0
94-96 1.25
91-93 1.5
88-90 1.75
85-87 2.0
82-84 2.25
79-81 2.5
76-78 2.75
75 3
74 3.25
71-73 3.5
68-70 3.75
65-67 4
64 down 5
SOURCE CODE 1:
SOURCE CODE 2:
o A function is a block of organized, reusable code that is used to perform a single, related action.
Functions provide better modularity for your application and a high degree of code reusing.
o As you already know, Python gives you many built-in functions like print(), etc. but you can also
create your own functions. These functions are called user-defined functions.
DEFINING A FUNCTION
You can define functions to provide the required functionality. Here are simple rules to define a function
in Python.
Function blocks begin with the keyword def followed by the function name and parentheses (( )).
Any input parameters or arguments should be placed within these parentheses. You can also define
parameters inside these parentheses.
The first statement of a function can be an optional statement - the documentation string of the
function or docstring.
The code block within every function starts with a colon (:) and is indented.
The statement return [expression] exits a function, optionally passing back an expression to the
caller. A return statement with no arguments is the same as return None.
CALLING A FUNCTION
o Defining a function only gives it a name, specifies the parameters that are to be included in the
function and structures the blocks of code.
o Once the basic structure of a function is finalized, you can execute it by calling it from another
function or directly from the Python prompt.
FUNCTION ARGUMENTS
You can call a function by using the following types of formal arguments –
• Required arguments
o Required arguments are the arguments passed to a function in correct positional order. Here,
the number of arguments in the function call should match exactly with the function definition.
• Keyword arguments
o Keyword arguments are related to the function calls. When you use keyword arguments in a
function call, the caller identifies the arguments by the parameter name.
o This allows you to skip arguments or place them out of order because the Python interpreter is
able to use the keywords provided to match the values with parameters.
• Default arguments
o A default argument is an argument that assumes a default value if a value is not provided in the
function call for that argument.
• Variable-length arguments
o You may need to process a function for more arguments than you specified while defining the
function. These arguments are called variable-length arguments and are not named in the
function definition, unlike required and default arguments.
o An asterisk (*) is placed before the variable name that holds the values of all nonkeyword
variable arguments. This tuple remains empty if no additional arguments are specified during
the function call.
Scope of Variables
All variables in a program may not be accessible at all locations in that program. This depends on where
you have declared a variable.
The scope of a variable determines the portion of the program where you can access a particular
identifier. There are two basic scopes of variables in Python −
Global variables
Local variables