Академический Документы
Профессиональный Документы
Культура Документы
Organization – University Of
Michigan
Website - Coursera
Reg No – RA1711003030524
Submitted To – Mr. Naresh Sharma
Branch/Section – CSE/I Designation -- Assistant Professor
Certificate
INDEX
1. Content
2. Motivation
3. About The Course
Content
1. Why we Program?
2. Variables
3. Statements
4. Operators and Operands
5. Expressions
6. Asking the user for Input
7. Conditional Code
8. Functions
9. Loops And Iteration
Motivation
Programming language have been around for ages , and every decade sees the launch of
a new language sweeping developers off their feet . Python is considered as one of the
most popular and in-demand programming language . A recent Stack Overflow survey
showed that Python has taken over languages such as Java , C , C++ and has made its way
to the top . This makes PYTHON Certification one of the most sought-after programming
certification . Through this I will be listing some of the most top reasons of me for
learning Python
The operators +, -, *, / and ** perform addition, subtraction, multiplication, division and exponentiation, as in the following
examples:
20+32
Hour=12
Minutes=10
Hour*60+minutes
(5+9)*(6+2)
>>>speed=10
>>>speed/=5
2
The Modulus Operator works on integer and yields the reminder when first operand is divided by the second.
>>>7%3
1
5. Expressions
An expression is a combination of values, variables, and operators. A value all by itself is considered
expression, and so is a variable, so the following are all legal expressions (assuming that the
variable x has
been assigned a value):
17
X
X+17
If you type an expression in interactive mode, the interpreter evaluates it and displays the result:
>>>1+1
2
6. Asking the user for input
Sometimes we would like to take the value for a variable from the user via their keyboard. Python
provide a built-in function called raw_input that gets input from the keyboard3. When this function is
called, the program stops and waits for the user to type something. When the user
presses Return or Enter, the program resumes and raw_input returns what the user typed as a string.
>>>input=raw_input(“Enter any thing”)
Xyx
>>>print(input)
Xyz
>>>name=input(“Enter your name”)
ABC
>>>print(name)
ABC
7. Conditional Code
1.And
For this both the expression should be True then it results True otherwise False.
Example
>>>X=2
>>>X>0 and X<=2
True
2. Or
For this one of the expression should be True then it results True otherwise False
>>>X=0
>>>X>0 or X<3
True
3.Not
This inverts the result . That is converts True to False and vise versa.
>>>X=False
>>>Print(!X)
True
7.3 Conditional Execution
In order to write useful programs, we almost always need the ability to check
conditions and change the behavior of the program accordingly. Conditional
statements give us this ability.
>>>if x>0:
print(“x is positive”)
7.5 Chained Conditionals
Sometimes there are more than two possibilities and we need more than two
branches. One way to express a computation like that is a chained
conditional:
If x<y:
print(“ less ”)
elif x>y:
print(“ greater ”)
else:
print(“equal”)
8. Functions
It is like a machine that has an input and an output. And the output is related somehow to the
input . 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.
Defining a Function
def function name (parameters):
“statements”
return [expression]
Example
X=2
def Square(x):
return x*x
//Function Calling
Y=Square (X)
Print(Y)
4
Why Functions?
•Dividing a long program into functions allows you to debug the parts one at a
time and then assemble them into a working whole.
•Well-designed functions are often useful for many programs. Once you write
and debug one, you can reuse it.
Built in Functions
Python provides a number of important built-in functions that we can use without needing to
provide the function definition. The creators of Python wrote a set of functions to solve common
problems and included them in Python for us to use.
Examples:
>>>max(“XY”)
‘Y’
>>>min(“XY”)
‘X’
>>>len(“XY”)
2
>>>random.randint(5,10)
6
>>>math.sqrt(2)
4
For using these built in functions we need to import modules like math and Random to use built
in Functions.
9. Iteration
These are used to execute same block of commands again and again.
Python has 2 Primitive loop commands:
1 while Loop
2 for Loop
While Loop
With the while loop we can execute a set of statements as long as a condition is
true.
Example:
i=1
While(i<6):
print(i)
I i+=1
Output:
12345
Break
With the break statement we can stop the loop even if the while condition is
true:
Example:
i=1
While(i<6):
print(i)
if i==3:
break
i+=1
Output:
12
Continue
With the continue statement we can stop the current iteration, and continue with the next:
Example:
i=1
While(i<6):
if i==3:
continue
print(i)
i+=1
Output:
1245
Else
With the else statement we can run a block of code once when the condition no
longer is true:
Example:
i=1
While(i<6):
print(i)
i+=1
Else:
print(“I is no longer less than 6”)
Output:
1 2 3 4 5 i is no longer less than 6
For Loop
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a
string).
This is less like the for keyword in other programming languages, and works more like an iterator
method as found in other object-orientated programming languages.
With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc.
Example:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
Output:
Apple banana cherry
Range() Function
for x in adj:
for y in fruits:
print(x, y)
Output:
red apple red banana red cherry