Python tutorial part 17 :- python if else statements

Python If Else Statements


 

The If statement is used to test specified condition and if the condition is true, if block executes, otherwise else block executes.The else statement executes when the if statement is false.

 

Python If Else Syntax

 

if(condition):  False  

             statements  

    else:   True  

              statements  

 



 

Example-

 

year=2000  


if year%4==0:  

    print  "Year is Leap"  

else:  

    print "Year is not Leap"  

Output:

Year is Leap

 

Python tutorial part 16 :- python if statements

Python If Statements


 


The Python if statement is a statement which is used to test specified condition. We can use if statement to perform conditional operations in our Python application. The if statement executes only when specified condition is true. We can pass any valid expression into the if parentheses.

 

There are various types of if statements in Python.

if statement


if-else statement


nested if statement


 

Python If Statement Syntax

if(condition):  

   statements  

Python If Statement Example

a=10  


if a==10:  


        print  "Welcome to ApkZube"  


Output:

Welcome to ApkZube

Python tutorial part 15:- Python Comments

Python Comments


 


Python supports two types of comments:


 


1) Single lined comment:


In case user wants to specify a single line comment, then comment must start with ?#?


 


Eg:


# This is single line comment.  


 


2) Multi lined Comment:


Multi lined comment can be given inside triple quotes.


 


eg:


''''' This 


    Is 

    Multipline comment'''  

 


eg:


#single line comment  


print "Hello Python"  

'''''This is 

multiline comment'''  

 


Python tutorial part 14 :- python Operators

Python Operators


 

Operators are particular symbols that are used to perform operations on operands. It returns result that can be used in application.

 

Example

1.    4 + 5 = 9  


 

Here 4 and 5 are Operands and (+) , (=) signs are the operators. This expression produces the output 9.

 

Types of Operators

Python supports the following operators

Arithmetic Operators.


Relational Operators.


Assignment Operators.


Logical Operators.


Membership Operators.


Identity Operators.


Bitwise Operators.


 

Arithmetic Operators

The following table contains the arithmetic operators that are used to perform arithmetic operations.

 

Operators

Description

//

Perform Floor division(gives integer value after division)

+

To perform addition

-

To perform subtraction

*

To perform multiplication

/

To perform division

%

To return remainder after division(Modulus)

**

Perform exponent(raise to power)

 

Example

>>> 10+20  


30  

>>> 20-10  

10  

>>> 10*2  

20  

>>> 10/2  

5  

>>> 10%3  

1  

>>> 2**3  

8  

>>> 10//3  

3  

>>>  

 

Relational Operators

The following table contains the relational operators that are used to check relations.

 

Operators


Description


Less than

Greater than

<=

Less than or equal to

>=

Greater than or equal to

==

Equal to

!=

Not equal to

<> 

Not equal to(similar to !=)

 

eg:

>>> 10<20  


True  

>>> 10>20  

False  

>>> 10<=10  

True  

>>> 20>=15  

True  

>>> 5==6  

False  

>>> 5!=6  

True  

>>> 10<>2  

True  

>>>  

 

Assignment Operators

The following table contains the assignment operators that are used to assign values to the variables.

 

Operators


Description


=

Assignment

/=

Divide and Assign

+=

Add and assign

-=

Subtract and Assign

*=

Multiply and assign

%=

Modulus and assign

**=

Exponent and assign

//=

Floor division and assign

 

Example

1.    >>> c=10  


2.    >>> c  

3.    10  

4.    >>> c+=5  

5.    >>> c  

6.    15  

7.    >>> c-=5  

8.    >>> c  

9.    10  

10. >>> c*=2  

11. >>> c  

12. 20  

13. >>> c/=2  

14. >>> c  

15. 10  

16. >>> c%=3  

17. >>> c  

18. 1  

19. >>> c=5  

20. >>> c**=2  

21. >>> c  

22. 25  

23. >>> c//=2  

24. >>> c  

25. 12  

26. >>>  

 

Logical Operators

The following table contains the arithmetic operators that are used to perform arithmetic operations.

 

Operators


Description


and

Logical AND(When both conditions are true output will be true)

or

Logical OR (If any one condition is true output will be true)

not

Logical NOT(Compliment the condition i.e., reverse)

 

Example

a=5>4 and 3>2  


print a  

b=5>4 or 3<2  

print b  

c=not(5>4)  

print c  

Output:

1.    >>>   

2.    True  

3.    True  

4.    False  

5.    >>>  

 

Membership Operators

The following table contains the membership operators.

 

Operators


Description


in

Returns true if a variable is in sequence of another variable, else false.

not in

Returns true if a variable is not in sequence of another variable, else false.

 

Example

a=10  


b=20  

list=[10,20,30,40,50];  

if (a in list):  

    print "a is in given list"  

else:  

    print "a is not in given list"  

if(b not in list):  

    print "b is not given in list"  

else:  

    print "b is given in list"  

Output:

>>>   

is in given list  

is given in list  

>>>  

 

Identity Operators

The following table contains the identity operators.

 

Operators


Description


is

Returns true if identity of two operands are same, else false

is not

Returns true if identity of two operands are not same, else false.

 

Example

a=20  


b=20  

if( a is b):  

    print  a,b have same identity  

else:  

    print a, b are different  

b=10  

if( a is not b):  

    print  a,b have different identity  

else:  

    print a,b have same identity  

Output

>>>   

a,b have same identity  

a,b have different identity  

>>>  

 

Python tutorial part 13:- python Literals

Python Literals


 


Literals can be defined as a data that is given in a variable or constant.


Python support the following literals:


 


I. String literals:


String literals can be formed by enclosing a text in the quotes. We can use both single as well as double quotes for a String.


 


Eg:


"Aman" , '12345'


 


Types of Strings:


There are two types of Strings supported in Python:


a).Single line String- Strings that are terminated within a single line are known as Single line Strings.


 


Eg:


1.    >>> text1='hello'  


 


b).Multi line String- A piece of text that is spread along multiple lines is known as Multiple line String.


There are two ways to create Multiline Strings:


 


1). Adding black slash at the end of each line.


Eg:


>>> text1='hello\  


user'  

>>> text1  

'hellouser'  

>>>  

 


2).Using triple quotation marks:-


Eg:


>>> str2='''''welcome 


to 

SSSIT'''  

>>> print str2  

welcome  

to  

SSSIT  

>>>  

 


 


II.Numeric literals:


 


Numeric Literals are immutable. Numeric literals can belong to following four different numerical types.


 


Int(signed integers)

Long(long integers)

float(floating point)

Complex(complex)

Numbers( can be both positive and negative) with no fractional part.eg: 100

Integers of unlimited size followed by lowercase or uppercase L eg: 87032845L

Real numbers with both integer and fractional part eg: -26.2

In the form of a+bj where a forms the real part and b forms the imaginary part of complex number. eg: 3.14j

 


III. Boolean literals:


A Boolean literal can have any of the two values: True or False.


 


IV. Special literals.


Python contains one special literal i.e., None.


None is used to specify to that field that is not created. It is also used for end of lists in Python.


 


Eg:


>>> val1=10  


>>> val2=None  

>>> val1  

10  

>>> val2  

>>> print val2  

None  

>>>  

 


V.Literal Collections.


Collections such as tuples, lists and Dictionary are used in Python.


 


List:


List contain items of different data types. Lists are mutable i.e., modifiable.


The values stored in List are separated by commas(,) and enclosed within a square brackets([]). We can store different type of data in a List.


Value stored in a List can be retrieved using the slice operator([] and [:]).


The plus sign (+) is the list concatenation and asterisk(*) is the repetition operator.


 


Eg:


>>> list=['aman',678,20.4,'saurav']  


>>> list1=[456,'rahul']  

>>> list  

['aman', 678, 20.4, 'saurav']  

>>> list[1:3]  

[678, 20.4]  

>>> list+list1  

['aman', 678, 20.4, 'saurav', 456, 'rahul']  

>>> list1*2  

[456, 'rahul', 456, 'rahul']  

>>>  

 


Python tutorial part 12 :- Identifiers

Identifiers


 


Identifiers are the names given to the fundamental building blocks in a program.

 

These can be variables ,class ,object ,functions , lists , dictionaries etc.There are certain rules defined for naming i.e., Identifiers.

 

1.    An identifier is a long sequence of characters and numbers.

2.    No special character except underscore ( _ ) can be used as an identifier.

3.    Keyword should not be used as an identifier name.

4.    Python is case sensitive. So using case is significant.

5.    .First character of an identifier can be character, underscore ( _ ) but not digit.

 

Python tutorial part 11 :- python keywords

Python Keywords


 


Python Keywords are special reserved words which convey a special meaning to the compiler/interpreter. Each keyword have a special meaning and a specific operation. These keywords can't be used as variable. Following is the List of Python Keywords.

 

True


False


None


and


as


asset


def


class


continue


break


else


finally


elif


del


except


global


for


if


from


import


raise


try


or


return


pass


nonlocal


in


not


is


lambda


 


Python tutorial part 10 :- python variables

Python Variables


 


Variable is a name which is used to refer memory location. Variable also known as identifier and used to hold value.In Python, we don't need to specify the type of variable because Python is a type infer language and smart enough to get variable type.

 

Variable names can be a group of both letters and digits, but they have to begin with a letter or an underscore.It is recomended to use lowercase letters for variable name. Rahul and rahul both are two different variables.

 

Note - Variable name should not be a keyword.


 

Declaring Variable and Assigning Values

Python does not bound us to declare variable before using in the application. It allows us to create variable at required time.

 

We don't need to declare explicitly variable in Python. When we assign any value to the variable that variable is declared automatically.

 

The equal (=) operator is used to assign value to a variable.

 

Eg:



 

Output:

>>>   

10  

ravi  

20000.67  

>>>  

 

Multiple Assignment

Python allows us to assign a value to multiple variables in a single statement which is also known as multiple assignment.We can apply multiple assignments in two ways either by assigning a single value to multiple variables or assigning multiple values to multiple variables. Lets see given examples.

 

1. Assigning single value to multiple variables

 

Eg:

x=y=z=50  


print x  

print y  

print z  

Output:

>>>   

50  

50  

50  

>>>  

 

2.Assigning multiple values to multiple variables:

Eg:

a,b,c=5,10,15  


print a  

print b  

print c  

Output:

>>>   

5  

10  

15  

>>>  

 

The values will be assigned in the order in which variables appears.

 

Basic Fundamentals:

This section contains the basic fundamentals of Python like :

i)Tokens and their types.

ii) Comments

 

a)Tokens:

Tokens can be defined as a punctuator mark, reserved words and each individual word in a statement.


Token is the smallest unit inside the given program.


 

There are following tokens in Python:

Keywords.


Identifiers.


Literals.


Operators.


 

Tuples:

Tuple is another form of collection where different type of data can be stored.


It is similar to list where data is separated by commas. Only the difference is that list uses square bracket and tuple uses parenthesis.


Tuples are enclosed in parenthesis and cannot be changed.


 

Eg:

>>> tuple=('rahul',100,60.4,'deepak')  


>>> tuple1=('sanjay',10)  

>>> tuple  

('rahul', 100, 60.4, 'deepak')  

>>> tuple[2:]  

(60.4, 'deepak')  

>>> tuple1[0]  

'sanjay'  

>>> tuple+tuple1  

('rahul', 100, 60.4, 'deepak''sanjay', 10)  

>>>  

 

Dictionary:

Dictionary is a collection which works on a key-value pair.


It works like an associated array where no two keys can be same.


Dictionaries are enclosed by curly braces ({}) and values can be retrieved by square bracket([]).


 

Eg:

>>> dictionary={'name':'charlie','id':100,'dept':'it'}  


>>> dictionary  

{'dept''it''name''charlie''id': 100}  

>>> dictionary.keys()  

['dept''name''id']  

>>> dictionary.values()  

['it''charlie', 100]  

>>>  

 

Quick Tech