PYTHON
PYTHON
From the basics:-.....What is Language?
-In General: The method of human communication, either spoken or written, consisting of the use of words in a structured and conventional way.
-In programming: A programming language is a formal language that specifies a set of instructions that can be used to produce various kinds of output. Programming languages generally consist of instructions for a computer. Programming languages can be used to create programs that implement specific algorithms.
Types:-
1. High-level language:-
HLLs are programming languages that look like natural language text.
They make programming easier and more abstract, i.e. the programmer does not need to come up with the detailed machine instructions.
HLL programs are machine independent. They can run on different hardware platforms (i.e. different computers with different instruction sets).
2. Low-level language:-
Low-level computer languages are machine codes or close to it. The computer cannot understand instructions given in high-level languages or in English. It can only understand and execute instructions given in the form of machine language i.e. language of 0 and 1.There are two types of low-level languages:
Machine Language:
It is the lowest and most elementary level of Programming language and was the first type of programming language to be Developed. Machine Language is basically the only language which the computer Can understand. In fact, a manufacturer designs a computer to obey just one Language, its machine code, which is represented inside the computer by a String of binary digits(bits) 0 and 1.
Assembly Language:
It was developed to overcome some of the many inconveniences of machine language. This is another low level but a very important language in which operation codes and operands are given in the form of alphanumeric symbols instead of 0’s and l’s. These alphanumeric symbols will be known as mnemonic codes and can have maximum up to 5 letter combination e.g. ADD for addition, SUB for subtraction, START, LABEL etc. Because of this feature, it is also known as ‘Symbolic Programming Language’. This language is also very difficult and needs a lot of practice to master it because very small English support is given to this language. The language mainly helps in compiler orientations. The instructions of the Assembly language will also be converted to machine codes by language translator to be executed by the computer.
Translator:-
Now the question will get arise that why we needed a translator in programming. Because of-A program written in the high-level language is called source code. To convert the source code into machine code, translators are needed. A translator takes a program written in source language as input and converts it into a program in the target language as output. It also detects and reports the error during translation.
S. NO. INTERPRETER COMPILER
1. It translates step by step. It translates the whole page
2. It shows error one by one. It shows error in the list at the end3. It's a SLOW translate It's a FAST translate
4. It generates intermediate code. It generates object code(in the form of 0 & 1).
•The difference between the concepts:: oop and pop:-
Procedure Oriented Programming. Object-Oriented Programming.
In POP, the program is divided into small In OOP, program is parts called functions. divided into parts called objects.
In POP,Importance is not given In OOP, Importance is given
to data but to functions as to the data rather than
well as sequence of actions procedures or functions to be done. because it works as a real world.
POP follows Top Down approach. OOP follows Bottom Up approach.
POP does not have any access OOP has access to specifiers
specifier. named Public,Private,
Protected, etc.
Protected, etc.
In POP, Data can move freely In OOP, objects can move
from function to function and communicate with each
in the system. other through member functions.
To add new data and function OOP provides an easy way in POP is not so easy. to add new data and function.
It is a computer programming language. Developed in 1989 by Mr. Guido van Rossum while working in a National Research Center at the Netherlands.
Officially python launch in 1991(publicly).When python gets develop then Mr. Guido van Rossum inherits most of the features of another language like:
1.Functional programming from - C.
2.Object-oriented programming concept(OOP'S)from - C++.
3.Modular programming from - Modulus.
4.Scripting feature from - Shell script.
There are some features like another language have, in python
1.Simple and general purpose language.
Easy to code & Easy to read.
Used in the development of web applications, desktop application.
Desktop application- Hardware based application.
Web application- Server based, creates dynamic pages.
2. Platform independent language.
It can be run on any hardware+software system.
3.Interpreter based(Interpreted).
In Python, there is no need to compile it.
Internally, its source code is converted into an immediate form called byte code.
4.Extensible.
It can be extended to another language.
If needed then you can write your python code in another language like C++, Java.
5. Embedded.
We can put our python code in a source code in different languages.
6.Free and open source.
Python is freely available.we can freely download it from its official site-www.python.org
It is open source it means that its codes are available to the public.
You can download it, change it, distribute it. This is called FLOSS(Free/Libre and Open Source Software).
7.Case sensitive.
Like other languages, there is a different meaning of alphabets A & a.
8.Dynamic language.
The type for a value is decided at the run time. There is no need to specify the type of data while declaring it.
9.Object Oriented.
It focuses on objects and combines data and functions.
10.Large standard library.
There are libraries for regular expressions, documentation-generation, unit-testing, web browsers,
threading, databases, CGI, email, image manipulation, and a lot of other functionality.
There are three versions of python:
1.Python-1.X (1994).
2.Python-2.X (2000).
3.Python-3.X (2008).
*Python does not provide backward compatibility.
There are some terminologies used in python:
1. Identifiers-The words that are used for identification purpose.
ex: Alphanumeric values-(a-z),(A,Z).Numeric values-(0-9).Underscore-(_)
In underscore(-):_a is known as private and __a is known as highly private.
2. Keywords-The words that are reserved for doing a specific task.
The keywords cannot be used as a variable, function or identifiers.
In python, keywords are case sensitive. All the keywords expect True, False and None all others are in lowercase form.
There are 33 keywords in up to python 3.3 and 35 in next versions.
3.Comment- It is used to increase the readability of the code in python.
For single line- # is get used before the sentence.like - #This is a comment.
For multiple lines- '''______ or """_____
______ _____
_____''' _____ """
4.Variable-Name of memory location,which is used to store a value.
ex- a=5 and b=10 here and b are the variables which are used to store the values 5 and 10.
5. Data type-They defines memory size and the type of a value of a variable can hold.
There are 14 data types in python.
(i)int
(ii)float
(iii)complex
(iv)str
(v)bool
(vi)bytes
(vii)byte array
(viii)range
(ix)list
(x)tuple
(xi)set
(xii)Frozen set
(xiii)Dictionary
(xvi)None
From (i) to (v) they are fundamental data types.
Now let's do some coding:
(1)simple programme to print anything-
print("thepj")
output- thepj
(2)For checking the type of a variable-
a=10
print(type(a))
b=1.5
print(type(b))
c=1+5j
print(type(c))
output- int
float
complex
(3)For taking a similar value for different variables-
a=b=c=10
print(type(a))
print(type(b))
print(type(c))
output- int
int
int
Now let's do some coding:
(1)simple programme to print anything-
print("thepj")
output- thepj
(2)For checking the type of a variable-
a=10
print(type(a))
b=1.5
print(type(b))
c=1+5j
print(type(c))
output- int
float
complex
(3)For taking a similar value for different variables-
a=b=c=10
print(type(a))
print(type(b))
print(type(c))
output- int
int
int
6. Slicing-It is used to print the exact array value related to that element.
ex: s="abcde"
-5 -4 -3 -2 -1
s > a b c d e
0 1 2 3 4
7 .Typecasting or conversion-When we change one data type to another.
Python providesa predicate function for type casting or type conversion.
Type casting function
-int()
-float()
-complex()
-str()
-bool()
-int: int function is used to convert another data type to int type.
*complex to int conversion is not possible.
(4)For converting float to int-
a=1.5
i=int(a)
print(i)
print(type(i))
output-1
int
(5)For converting bool to int-
a= True
i=int(a)
print(i)
print(type(i))
output-1
int
a=False
i=float(a)
print(i)
print(type(i))
output- 0
float
(6)For converting srting to int-
*A srting is should be in the form of int.
a="10"
i=int(a)
print(i)
print(type(i))
output-10
-float: float() function is used to convert another data type to float type
*complex to float conversion is not possible.
(7)For converting int to float-
a=10
i=float(a)
print(i)
print(type(i))
output- 10.0
float
(8)For converting bool to float-
a=True
i=float(a)
print(i)
print(type(i))
output- 1.0
float
a=False
i=float(a)
print(i)
print(type(i))
output- 0.0
float
(9)For converting string to float-
*A string is should be in the form of int or float.
a="10"
i=float(a)
print(i)
print(type(i))
output- 10.0
float
a="10.5"
i=float(a)
print(i)
print(type(i))
output- 10.5
float
-complex: complex() function is used to convert another data type to complex type.
(10)For converting int to complex-
a=10
i=complex(a)
print(i)
print(type(i))
output- 10+0j
complex
(11)For converting float to complex-
a=10.5
i=complex(a)
print(i)
print(type(i))
output- 10.5+0j
complex
(12)For converting bool to complex-
a=True
i=complex(a)
print(i)
print(type(i))
output- 1+0j
complex
a=False
i=complex(a)
print(i)
print(type(i))
output- 0j
complex
-bool: bool() function is used to convert another data type to bool type.
(13)For converting int to bool-
*All non zero values are true.
a=10
i=bool(a)
print(i)
print(type(i))
output- True
bool
a=0
i=bool(a)
print(i)
print(type(i))
output- False
bool
(14)For converting float to bool-
a=1.5
i=bool(a)
print(i)
print(type(i))
output- True
bool
a=0.0
i=bool(a)
print(i)
print(type(i))
output- False
bool
(15)For converting complex to bool-
a=1+5j
i=bool(a)
print(i)
print(type(i))
output- True
bool
a=0+0j
i=bool(a)
print(i)
print(type(i))
output- False
bool
*All fundamental(int,float,complex,str & bool) are immutable(Non-changeable).
(16) #Same address for the different variable if their values are same.
a=10
b=10
c=10
print(id(a))
print(id(b))
print(id(c))
output- 2012996512
2012996512
2012996512
#Now if the value for any varible will get change then it's id also gets change.
a=10
b=10
print(id(a))
print(id(b))
a=20
print(id(a))
output- 2012996512
2012996512
2012996672
#Use of is to swap the values between two variables.
a=10
b=10
c=10
print(a is b)
a=20
print(c is a)
output- True
False
8.Operator- It's a symbol that specify the types of operations possible an operands or variables.
In python the different operators are-
(i)Arithmetic.
(ii)Logical.
(iii)Relational.
(i)Assignment.
(i)Bit-wise.
(i)Special.
*Python does not support Unary and Terminory operators.
(i)Arithmetic operators-
+ --- Addition
- --- Subtraction
* --- Multiplication
/ --- Division
% --- Modulo
// --- Floor division
** --- Exponent
(18) x = 15
y = 4
# Output: x + y = 19
print('x + y =',x+y)
# Output: x - y = 11
print('x - y =',x-y)
# Output: x * y = 60
print('x * y =',x*y)
# Output: x / y = 3.75
print('x / y =',x//y)
# Output: x // y = 3
print('x // y =',x//y)
# Output: x ** y = 50625
print('x ** y =',x**y)
(19)Difference between the addition and concatenation-
#(Addition)
print(5+5)
#(Contention)
print("Hello"+"students")
output- 10
Hello students
-string with int is not applicable.It will show error.
(ii)Logical operators-
-and : True if both the operands are true
-or : True if either of the operands is true
-not : True if the operand is false (complements the operand)
In logical operators-
#For and operator-
True and True - True
True and False - False
False and True - False
False and False - False
#For or operator-
True or True - True
True or False - True
False or True - True
False or False - False
(20)print("Hello" and "Hye")
print(10 and 20)
print("" and "Hye")
print(0 and 20)
output- Hye
20
0
(21)print("Hello" or "Hye")
print(10 or 20)
print("" or "Hye")
print(0 or 20)
output-Hello
10
Hye
20
(iii)Relational-
-Greater than : < True if left operand is greater than the right
-Greater than equal to : <= True if left operand is greater than or equal to the right
-Less than : > True if left operand is less than the right
-Less than equal to : >= True if left operand is less than or equal to the right
-Equal to : == True if both operands are equal
-Not equal to : != True if operands are not equal
(22)a=10
b=20
print(a>b)
print(a>=b)
print(a<b)
print(a<=b)
output- False
False
True
True
(23)#In str the relation is written according to the comparison of first to first and so on. (letter by letter)
a="Hello"
b="thepj"
#compare H with t and so on
print(a>b)
print(a>=b)
print(a<b)
print(a<=b)
Output- False
False
True
True
(iv)Assighnment -
Operator Example Equivalent
= x = 5 x = 5
+= x += 5 x = x + 5
-= x -= 5 x = x - 5
*= x *= 5 x = x * 5
/= x /= 5 x = x / 5
%= x %= 5 x = x %5
//= x //= 5 x = x // 5
**= x **= 5 x = x ** 5
&= x &= 5 x = x & 5
|= x |= 5 x = x | 5
^= x ^= 5 x = x ^ 5
>>= x >>= 5 x = x >> 5
<<= x <<= 5 x = x << 5
(v)Bit-wise operator-
Operator Meaning Example
& Bit-wise AND x& y = 0 (0000 0000)
| Bit-wise OR x | y = 14 (0000 1110)
~ Bit wise NOT ~x = -11 (1111 0101)
^ Bit-wise XOR x ^ y = 14 (0000 1110)
>> Bit-wise right shift x>> 2 = 2 (0000 0010)
<< Bit-wise left shift x<< 2 = 40 (0010 1000)
(vi)Special operator-
1.Identity: It is used for address(reference)comparison purpose.
Operator Meaning Example
is True if the operands are identical x is True
(refer to the same object)
is not True if the operands are not identical x is not True
(do not refer to the same object)
2.Membership : To check the object is in list or not.
Operator Meaning Example
in True if the element is existed in the list. x is True
not in True if the element is not existed in the list. x is not True
(24) x1 = 5
y1 = 5
x2 = 'Hello'
y2 = 'Hello'
x3 = [1,2,3]
y3 = [1,2,3]
# Output: False
print(x1 is not y1)
# Output: True
print(x2 is y2)
# Output: False
print(x3 is y3)
(24)l=[1,2,3,4,5]
#Output: list
print(type(l))
#Output:False
print(7 in l)
#Output:True
print(5 in l)
-Methods to take input from the user :
python-2.x
1.raw_input()
it always work as a string,
for particular data type we have to use -
for int - int(raw_input)
for float - float(raw_input)
similarly for others.
#without declaring type(always a str)
a=raw_input("Enter the value of a=")
b=raw_input("Enter the value of b=")
print(a+b)
Output- Enter the value of a=10
Enter the value of b=20
1020
#By declaring type
a=int(raw_input("Enter the value of a="))
b=int(raw_input("Enter the value of b="))
print(a+b)
Output- Enter the value of a=10
Enter the value of b=20
30
2.input()
a=input("Enter the value of a=")
b=input("Enter the value of b=")
print(a+b)
Output - a = 10
b = 20
c = 30
Python - 3.x
1.input()
In python 3.x the input function without declaring type is always work as a string.
a = input()
b = input()
print(type(a))
print(type(b))
print("Sum = ",a+b)
Output - str
str
10
20
Sum = 1020
For declaring the type we have a common function eval() which is automatically detect the data type and write the value in that particular form.
a = eval(input())
b = eval(input())
print(type(a))
print(type(b))
print("Add = ",a+b)
Output - 12
13
int
int
Sum = 25
* Replacement perator -
# represented by -{}
a = 10
b = 20
print("Sum of {} and {} is {}".format(a,b,a+b))
print("Sum of {x} and {y} is {z}".format(x=a,y=b,z=a+b))
print("Sum of {0} and {1} is {2}".format(a,b,a+b))
Output - Sum of 10 and 20 is 30
Sum of 10 and 20 is 30
Sum of 10 and 20 is 30
Now the important concept of programming the control statement-
|
--------------------------------------------------------------------------------
| | |
Selective/Conditional Iterative/Looping Transfer/Jump
-- if-else -- while -- break
-- elif -- continue
-- pass
switch case is not do-while is not goto statement is not applicable here. applicable here. in python.
1. if statement- 2.if-else -
if cond: if cond:
statement..... statement.....
else:
statemnet.....
1)num=eval(input("enter a number"))
if num>0:
print(num,"+ve number")
2)num1=eval(input("Enter 1st number"))
num2=eval(input("Enter 2nd number"))
num3=eval(input("Enter 3rd number"))
if num1>num2 and num1>num3:
print("Number 1st is greater")
elif num2>num3:
print("Number 2nd is greater")
ex: s="abcde"
-5 -4 -3 -2 -1
s > a b c d e
0 1 2 3 4
7 .Typecasting or conversion-When we change one data type to another.
Python providesa predicate function for type casting or type conversion.
Type casting function
-int()
-float()
-complex()
-str()
-bool()
-int: int function is used to convert another data type to int type.
*complex to int conversion is not possible.
(4)For converting float to int-
a=1.5
i=int(a)
print(i)
print(type(i))
output-1
int
(5)For converting bool to int-
a= True
i=int(a)
print(i)
print(type(i))
output-1
int
a=False
i=float(a)
print(i)
print(type(i))
output- 0
float
(6)For converting srting to int-
*A srting is should be in the form of int.
a="10"
i=int(a)
print(i)
print(type(i))
output-10
-float: float() function is used to convert another data type to float type
*complex to float conversion is not possible.
(7)For converting int to float-
a=10
i=float(a)
print(i)
print(type(i))
output- 10.0
float
(8)For converting bool to float-
a=True
i=float(a)
print(i)
print(type(i))
output- 1.0
float
a=False
i=float(a)
print(i)
print(type(i))
output- 0.0
float
(9)For converting string to float-
*A string is should be in the form of int or float.
a="10"
i=float(a)
print(i)
print(type(i))
output- 10.0
float
a="10.5"
i=float(a)
print(i)
print(type(i))
output- 10.5
float
-complex: complex() function is used to convert another data type to complex type.
(10)For converting int to complex-
a=10
i=complex(a)
print(i)
print(type(i))
output- 10+0j
complex
(11)For converting float to complex-
a=10.5
i=complex(a)
print(i)
print(type(i))
output- 10.5+0j
complex
(12)For converting bool to complex-
a=True
i=complex(a)
print(i)
print(type(i))
output- 1+0j
complex
a=False
i=complex(a)
print(i)
print(type(i))
output- 0j
complex
-bool: bool() function is used to convert another data type to bool type.
(13)For converting int to bool-
*All non zero values are true.
a=10
i=bool(a)
print(i)
print(type(i))
output- True
bool
a=0
i=bool(a)
print(i)
print(type(i))
output- False
bool
(14)For converting float to bool-
a=1.5
i=bool(a)
print(i)
print(type(i))
output- True
bool
a=0.0
i=bool(a)
print(i)
print(type(i))
output- False
bool
(15)For converting complex to bool-
a=1+5j
i=bool(a)
print(i)
print(type(i))
output- True
bool
a=0+0j
i=bool(a)
print(i)
print(type(i))
output- False
bool
*All fundamental(int,float,complex,str & bool) are immutable(Non-changeable).
(16) #Same address for the different variable if their values are same.
a=10
b=10
c=10
print(id(a))
print(id(b))
print(id(c))
output- 2012996512
2012996512
2012996512
#Now if the value for any varible will get change then it's id also gets change.
a=10
b=10
print(id(a))
print(id(b))
a=20
print(id(a))
output- 2012996512
2012996512
2012996672
#Use of is to swap the values between two variables.
a=10
b=10
c=10
print(a is b)
a=20
print(c is a)
output- True
False
8.Operator- It's a symbol that specify the types of operations possible an operands or variables.
In python the different operators are-
(i)Arithmetic.
(ii)Logical.
(iii)Relational.
(i)Assignment.
(i)Bit-wise.
(i)Special.
*Python does not support Unary and Terminory operators.
(i)Arithmetic operators-
+ --- Addition
- --- Subtraction
* --- Multiplication
/ --- Division
% --- Modulo
// --- Floor division
** --- Exponent
(18) x = 15
y = 4
# Output: x + y = 19
print('x + y =',x+y)
# Output: x - y = 11
print('x - y =',x-y)
# Output: x * y = 60
print('x * y =',x*y)
# Output: x / y = 3.75
print('x / y =',x//y)
# Output: x // y = 3
print('x // y =',x//y)
# Output: x ** y = 50625
print('x ** y =',x**y)
(19)Difference between the addition and concatenation-
#(Addition)
print(5+5)
#(Contention)
print("Hello"+"students")
output- 10
Hello students
-string with int is not applicable.It will show error.
(ii)Logical operators-
-and : True if both the operands are true
-or : True if either of the operands is true
-not : True if the operand is false (complements the operand)
In logical operators-
#For and operator-
True and True - True
True and False - False
False and True - False
False and False - False
#For or operator-
True or True - True
True or False - True
False or True - True
False or False - False
(20)print("Hello" and "Hye")
print(10 and 20)
print("" and "Hye")
print(0 and 20)
output- Hye
20
0
(21)print("Hello" or "Hye")
print(10 or 20)
print("" or "Hye")
print(0 or 20)
output-Hello
10
Hye
20
(iii)Relational-
-Greater than : < True if left operand is greater than the right
-Greater than equal to : <= True if left operand is greater than or equal to the right
-Less than : > True if left operand is less than the right
-Less than equal to : >= True if left operand is less than or equal to the right
-Equal to : == True if both operands are equal
-Not equal to : != True if operands are not equal
(22)a=10
b=20
print(a>b)
print(a>=b)
print(a<b)
print(a<=b)
output- False
False
True
True
(23)#In str the relation is written according to the comparison of first to first and so on. (letter by letter)
a="Hello"
b="thepj"
#compare H with t and so on
print(a>b)
print(a>=b)
print(a<b)
print(a<=b)
Output- False
False
True
True
(iv)Assighnment -
Operator Example Equivalent
= x = 5 x = 5
+= x += 5 x = x + 5
-= x -= 5 x = x - 5
*= x *= 5 x = x * 5
/= x /= 5 x = x / 5
%= x %= 5 x = x %5
//= x //= 5 x = x // 5
**= x **= 5 x = x ** 5
&= x &= 5 x = x & 5
|= x |= 5 x = x | 5
^= x ^= 5 x = x ^ 5
>>= x >>= 5 x = x >> 5
<<= x <<= 5 x = x << 5
(v)Bit-wise operator-
Operator Meaning Example
& Bit-wise AND x& y = 0 (0000 0000)
| Bit-wise OR x | y = 14 (0000 1110)
~ Bit wise NOT ~x = -11 (1111 0101)
^ Bit-wise XOR x ^ y = 14 (0000 1110)
>> Bit-wise right shift x>> 2 = 2 (0000 0010)
<< Bit-wise left shift x<< 2 = 40 (0010 1000)
(vi)Special operator-
1.Identity: It is used for address(reference)comparison purpose.
Operator Meaning Example
is True if the operands are identical x is True
(refer to the same object)
is not True if the operands are not identical x is not True
(do not refer to the same object)
2.Membership : To check the object is in list or not.
Operator Meaning Example
in True if the element is existed in the list. x is True
not in True if the element is not existed in the list. x is not True
(24) x1 = 5
y1 = 5
x2 = 'Hello'
y2 = 'Hello'
x3 = [1,2,3]
y3 = [1,2,3]
# Output: False
print(x1 is not y1)
# Output: True
print(x2 is y2)
# Output: False
print(x3 is y3)
(24)l=[1,2,3,4,5]
#Output: list
print(type(l))
#Output:False
print(7 in l)
#Output:True
print(5 in l)
-Methods to take input from the user :
python-2.x
1.raw_input()
it always work as a string,
for particular data type we have to use -
for int - int(raw_input)
for float - float(raw_input)
similarly for others.
#without declaring type(always a str)
a=raw_input("Enter the value of a=")
b=raw_input("Enter the value of b=")
print(a+b)
Output- Enter the value of a=10
Enter the value of b=20
1020
#By declaring type
a=int(raw_input("Enter the value of a="))
b=int(raw_input("Enter the value of b="))
print(a+b)
Output- Enter the value of a=10
Enter the value of b=20
30
2.input()
a=input("Enter the value of a=")
b=input("Enter the value of b=")
print(a+b)
Output - a = 10
b = 20
c = 30
Python - 3.x
1.input()
In python 3.x the input function without declaring type is always work as a string.
a = input()
b = input()
print(type(a))
print(type(b))
print("Sum = ",a+b)
Output - str
str
10
20
Sum = 1020
For declaring the type we have a common function eval() which is automatically detect the data type and write the value in that particular form.
a = eval(input())
b = eval(input())
print(type(a))
print(type(b))
print("Add = ",a+b)
Output - 12
13
int
int
Sum = 25
* Replacement perator -
# represented by -{}
a = 10
b = 20
print("Sum of {} and {} is {}".format(a,b,a+b))
print("Sum of {x} and {y} is {z}".format(x=a,y=b,z=a+b))
print("Sum of {0} and {1} is {2}".format(a,b,a+b))
Output - Sum of 10 and 20 is 30
Sum of 10 and 20 is 30
Sum of 10 and 20 is 30
Now the important concept of programming the control statement-
CONTROL STATEMENT
|
--------------------------------------------------------------------------------
| | |
Selective/Conditional Iterative/Looping Transfer/Jump
statement statement statement -- if -- for -- return
-- if-else -- while -- break-- elif -- continue
-- pass
switch case is not do-while is not goto statement is not applicable here. applicable here. in python.
1. if statement- 2.if-else -
if cond: if cond:
statement..... statement.....
else:
statemnet.....
1)num=eval(input("enter a number"))
if num>0:
print(num,"+ve number")
elif num<0: print(num,"-ve number") else: print("number is 0")
2)num1=eval(input("Enter 1st number"))
num2=eval(input("Enter 2nd number"))
num3=eval(input("Enter 3rd number"))
if num1>num2 and num1>num3:
print("Number 1st is greater")
elif num2>num3:
print("Number 2nd is greater")
else:
print("Number 3rd is greater")
print("Number 3rd is greater")
LOOPING OR ITERATIVE-
The for loop in python is used to iterate over a sequence(list, tuples, string) or other iterable objects. Iterating over a sequence is called traversal.
1)l = [1,2,3,4,5] for i in l: print(i,end="-")
2)for i in range(10) print(i,end=" ")
3)a=int(input("enter a number"))
for i in range(11):
print(a*i)
4)Count=0
s="Hello"
for i in s:
count +=1
print(i)
print("total char=",count)
For Loop With Else-
A for loop can have an optional else block as well. The else part is executed if the items in the sequence used in for loop exhausts. Break statement can be used to stop a for a loop. In such carrier, the else the part is ignored.
1) l=[1,2,3,4,5,6,7]
for i in l:
if i==5:
break
print(i,end=" ")
else:
print("No item left")
2)#Marksheet programme
name= input("Enter the name of the student=")
sub1= int(input("Enter the marks of english="))
sub2= int(input("Enter the marks of Hindi="))
sub3= int(input("Enter the marks of Science="))
sub4= int(input("Enter the marks of Computer Science="))
sub5= int(input("Enter the marks of Sociel Studies="))
sum= int((sub1+sub2+sub3+sub4+sub5))
print= ("sum of the total marks out of 500=",sum)
avg = sum/5
percentage= (sum/500)*100
print=("Total percentage=",avg)
if(avg>=90):
print("Grade= A")
elif(avg>=75 and avg<90):
print("Grade:B")
elif(avg>=60 and avg<75):
print("Grade:C")
elif(avg>=55 and avg<60):
print("Grade:D")
else:
print("Grade:F")
sub1= int(input("Enter the marks of english="))
sub2= int(input("Enter the marks of Hindi="))
sub3= int(input("Enter the marks of Science="))
sub4= int(input("Enter the marks of Computer Science="))
sub5= int(input("Enter the marks of Sociel Studies="))
sum= int((sub1+sub2+sub3+sub4+sub5))
print= ("sum of the total marks out of 500=",sum)
avg = sum/5
percentage= (sum/500)*100
print=("Total percentage=",avg)
if(avg>=90):
print("Grade= A")
elif(avg>=75 and avg<90):
print("Grade:B")
elif(avg>=60 and avg<75):
print("Grade:C")
elif(avg>=55 and avg<60):
print("Grade:D")
else:
print("Grade:F")
Thankyou for the knowledge 😃..it will help many people like me
ReplyDeletePlease Post more information 😃
ReplyDelete