Вы находитесь на странице: 1из 27

Subject Code - 15CS385L

Subject Name – MOOC’S

Course Name – Python for


Everybody

Organization – University Of
Michigan

Website - Coursera

Submitted By – Poorv Mishra

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

1 Python’s Popularity and High Salary


2 Python is used in AI
3 Python is used in Data Science
4 Python used with BigData
About The Course
1. Why we Program ?
Writing programs (or programming) is a very creative and
rewarding activity. You can write programs for many
reasons ranging from making your living to solving a
difficult data analysis problem to having fun to helping
someone else solve a problem.
The definition of a program at its most basic is a sequence of
Python statements that have been crafted to do
something. 
2. Variables
One of the most powerful features of a programming language is the
ability to manipulate variables.
A variable is a name that refers to a value.
>>> n=7
>>> print(n)
7
Programmers generally choose names for their variables that are
meaningful---
they document what the variable is used for.
Variable names can be arbitrarily long. They can contain both letters
and numbers, but they have to begin with a letter. It is legal to use
uppercase letters, but it is a good idea to begin variable names with a
lowercase letter.
The underscore character (_) can appear in a name. It is often used in
names with multiple words, such as  my _ name.
3. Statements
A statement is a unit of code that the Python interpreter can execute. We have seen two
kinds of statements: print and assignment . When you type a statement in interactive
mode, the interpreter executes it and displays the result, if there is one. A script usually
contains a sequence of statements. If there is more than one statement, the results
appear one at a time as the statements execute.
Example
Print(1)
X=2
Print(x)
Produces output-
1
2
4. Operators and Operands
Operators are special symbols that represent computations like addition and multiplication.
 The values the operator is applied to are called operands.

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

7.1 Boolean Expression


A Boolean Expression is an expression that is either true or false.
Example
>>>5==5
True
>>>5>=6
False
Comparison Operators:
1. x!=y
2. x>y
3. x<y
4. x>=y
5. x is y
6. x is not y
7.2 Logical Operator
There are three logical operators: and, or, and not.

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”)

The Boolean expression after if statement is called condition.


7.4. Alternative Execution
A second form of the if statement is alternative execution, in which there are two possibilities and the 
condition determines which one gets executed.
If x%2==0:
print(“x is even”)
else:
print(“x is odd”)

 
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?

•Creating a new function gives you an opportunity to name a group of


statements, which makes your program easier to read, understand and debug.

•Functions can make a program smaller by eliminating repetitive code. Later, if


you make a change, you only have to make it in one place.

•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

To loop through a set of code a specified number of times, we can use


the range() function,
The range() function returns a sequence of numbers, starting from 0 by default,
and increments by 1 (by default), and ends at a specified number.
Example:
for x in range(6):
  print(x)
Output:
012345
Nested Loops

A nested loop is a loop inside a loop.


The "inner loop" will be executed one time for each iteration of the "outer loop":
Example:
adj = ["red"]
fruits = ["apple", "banana", "cherry"]

for x in adj:
  for y in fruits:
    print(x, y)
Output:
red apple red banana red cherry

Вам также может понравиться