Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

Python Bit

Uploaded by

Mohammed Razi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Python Bit

Uploaded by

Mohammed Razi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Statements ● Else Condition:-The “else condition” is Nested IF statements Expressions in Python Functions in Python

usually used when you have to judge


Instructions written in the source one Statement on the basis of other. If There may be a situation when you An expression is a combination of values,
one condition goes wrong, then There want to check for another condition variables, operators, and calls to  A function is a block of
code for execution are called
Statements. should be another condition that should after a Condition resolves to true. In Functions. Expressions need to be organized, reusable code that
justify the statement or Logic. Syntax: such a situation, you can use the evaluated. If you ask Python to print an
expression, the Interpreter evaluates the is used to perform a single,
There are different types of nested if construct. In a nested if
statements in the Python
If expression
construct, you can have an
expression and displays the result. related Action.
programming Language like Statement if...elif...else construct inside another Print(1 + 1)  Functions provide better
Assignment statement, If...elif...else construct.
Else Print(len(“hello”)) modularity for your application
Conditional statement, Looping
statements etc. These all help the
Syntax:-The syntax of the nested and a high degree of code
Statement if...elif...else construct may be – Most statements (logical lines) that you
user to get the required output. write will contain expressions. A simple Reusing.
Example: If expression1: Example of an expression is 2 + 3. An  It is a group of related
Assignment statement :- An expression can be broken down into
assignment statement evaluates X=2 Statement(s) operators And operands statements that perform a
the expression list and assigns specific task functions help
Y=8 If expression2: Features of Python
The single resulting object to each
of the target lists, from left to
break our Program.
If(x < y):  Easy to code and read
Statement(s)
Right.  Open- source
 A function is a block of code
Print(“x is less than y”)
Elif expression3: Eg:- linux is free to use, and you can which only runs when it is
For example, n = 50 is an make changes. Anyone can create
assignment statement.
Else:
Statement(s) and contribute to that.
called.
Print(“x is greater than y”)  Developer friendly  A function is a set of
Conditional statement :- Elif expression4:  Graphical User Interface (GUI) and
Conditional Statement in Python  “elif” condition database support
statements that take inputs,
perform different computations Statement(s)  Object oriented programming do some specific computation
or Actions depending on whether By using “elif” condition, you are telling  High level programming
the program to print out the third Else: and Produces output.
a specific Boolean constraint  Interpreted language by nature
Condition or possibility when the other  Highly portable
evaluates to True or false. condition goes wrong or incorrect. Statement(s)
 Scalable
Conditional statements are
Else:  Extensible
handled by IF, ELSE, ELIF,Nested Example:
 Dynamically accepts Defining a Function
IF & Switch Case Statements .
x=2 Statement(s)
● IF Condition :- In Python, If  You can define functions to
y=8 Switch Statement:-
Statement is used for decision provide the required
making. It will run the body of if(x < y): A switch statement is a multiway functionality. Here are simple
Code only when IF statement is branch statement that compares the
true. When you want to justify print(“x is less than y”) valueOf a variable to the values rules to define A function in
one condition. While the other specified in case statements.Python Python.
elif(y<z): language doesn’t have a switch
condition is not true, then you  Function blocks begin with the
statements.Python uses dictionary
use “if statement”. print(“z is greater than y”)
mapping to implement switch keyword def followed by the
else: statement in Python
Syntax: function name and
Example:-
If expression print(“y is greater than z”) Parentheses ( ( ) ).
Statement
Function(argument){  Any input parameters or
Switch(argument) { arguments should be placed
Example:
Case 0:
within these parentheses. You
X=2 Can also define parameters
Return “This is Case Zero”;
Y=8 inside these parentheses.
Case 1:  The first statement of a
If(x < y):
Return “ This is Case One”; function can be an optional
Print(“x is less than y”) statement – the
Case 2:
documentation String of the
Return “ This is Case Two “;
function or docstring.
Default:
Types of Functions  The code block within every
Return “nothing”; function starts with a colon (
1. Built-in Functions and is indented.
 The Python built-in functions are defined as the functions whose
};
functionality is Pre-defined in Python.
 The python interpreter has several functions that are always present for };
use.
 These functions are known as Built-in Functions.
 They are pieces of code that are available for you to use in your programs.
 Python has several functions that are readily available for use.
 Some built-in functions in Python which are listed below:

2. User-Defined Functions
 A function that you define yourself in a program is known as user defined
Function.
 You can give any name to a user defined function, however you cannot use
The Python keywords as function name.
 In python, we define the user-defined function using def keyword, followed
By the function name.
 Function name is followed by the parameters in parenthesis, followed by the
Colon.
 For example:
Def function_name(parameter_1, parameter_2, ...) :
Statements
...

Advantages of user-defined functions

1. User-defined functions help to decompose a large program into small


Segments which makes program easy to understand, maintain and debug.
2. If repeated code occurs in a program. Function can be used to include those
Codes and execute when needed by calling that function.
3. Programmers working on large project can divide the workload by making
Different functions.

You might also like