PYTHON - 1
PYTHON - 1
- By SIMRAN KAUR(CSE-G7)
Like assignment operator, there are other operators which can be used to perform various operations.
2. Relational operators: Also known as comparison operators are used to compare values. Result of a
relational expression is always either true or false.
3. Logical operators are used to combine one or more relational expressions.
DATATYPES IN PYTHON-
Python supports following data types-
e.g. -
NOTE--Here, print() function is used to print the type of data type using type() function.
PRECEDENCE –
Precedence of an operator can be identified based on the rule - BODMAS. Brackets followed by Orders
(Powers, Roots), followed by modulo, Division and Multiplication, followed by Addition and Subtraction.
2. Modulo, Division and Multiplication have the same precedence. Hence if all appear in an expression, they
are evaluated from Left to Right.
3. 3. Addition and Subtraction have the same precedence. Hence if both appear in an expression, they are
evaluated from Left to Right.
IMPLICIT CONVERSION –
Take a look at the code: num=1 + 1.0 #interpreter assumed 1 s 1.0
EXPLICIT CONVERSION –
Take a look at the below code: num=1 + int(1.0) #1.0 converted to 1
NOTE-
*Converting a floating point value to integer would result in loss of decimal point values.
*A larger data type if converted to smaller data type will result in loss of data as the number will
be truncated.
ESCAPE SEQUENCES –
1. \n = next line
2. \t = tab space
3. \” = “ in string
4. \\\\ = \\ and \\ = \
5. , end=" " = joins strings of 2 different print statements
FUNCTIONS –
A function is a block of code that performs a particular task. In python, functions are declared using the
keyword def. INDENTATION is very important in programs.
#All the statements in the block of code must have the same level of indentation
result_sum = data1+data2
return result_sum
result = calculate_sum(10,20)
print(result)
Function call
“RETURN” in FUNCTION –
Do this
else: do this
#use of loops
‘BREAK’ STATEMENT – When we want to stop a loop or break away from it we can use break statement.
‘CONTINUE’ STATEMENT - When we want to skip the remaining portion of loop statements and continue with the next
iteration, we can use continue statement.
ECLIPSE –
Eclipse is a modern IDE. IDE stands of Integrated Development Environment, where all the necessary tools for developing your code
are integrated into one piece.
LIST IN PYTHON –
It can be used to store a group of elements together in a sequence.
# The list index starts from zero. Index positions actually help us to directly access a value from the list.
# list_name[index] can be used to directly access the list element at the mentioned index position.
# We can also use it to directly modify an element in the list. e.g. - ticket_list[3]=13504
# We cannot access values beyond the total number of elements in the list. e.g. - print(ticket_list[5]) will result in
index out of bound error.
list_of_airlines=["AI","EM","BA"]
** If we just want to find out whether an element is there in a list, we need not iterate through the list. Instead
we can check using if..in syntax.
list_of_airlines=["AI","EM","BA"]
airline="AI"
if airline in list_of_airlines:
print("Airline found")
else:
SLICING IN LISTS –
e.g. -
sub_list = list_of_airlines[1:4] provides a sub list from index position 1 to 3 (i.e., 1 to (4-1)).
Indices may also be considered negative as shown above. This is normally used to count from right.
For example: To fetch the second last airline in the list, we can write list_of_airlines[-2].
## If we want to store the airline details of all airlines operating from an airport, we may use a list of lists as
shown in the code below.
LIST FUNCTIONS –
TUPLE IN PYTHON –
Like list, tuple can also store a sequence of elements but the value of the elements cannot be changed.
(i.e. tuples are IMMUTABLE). Elements can be homogeneous or heterogeneous but they are
READ-ONLY.
Suppose it is mandatory to have the following types of food in the lunch menu of the passengers.
Welcome Drink, Veg Starter, Non-Veg Starter, Veg Main Course, Non-Veg Main Course, Dessert
STRING IN PYTHON –
Alphabetical or alpha numerical values are called strings. Each value in a string is called a character.
Just like list elements, we can access the characters in a string based on its index position.
In Python, string is a data type and anything enclosed in a single quote or double quote is considered to
be a string. All the remaining operations are similar to lists. But like tuple, strings are also IMMUTABLE.
1. Length of string
2. Slicing a string
3. Concatenating two strings
STRING FUNCTIONS –
CHOOSING BETWEEN LIST, TUPLE AND STRING –
row1 = (101,"Dallas",3.5)
row2 = (102,"Atlanta",5.6)
row3 = (103,"Tokyo",9.8)
table = [row1,row2,row3]
### Here,
One of the advantage of using dictionary is that it allows very fast search for value based on key.
The key should be unique and can be of any immutable data type.
SETS IN PYTHON –
A set is an unordered group of values with no duplicate entries.
Set can be created by using the keyword set or by using curly braces {}.
The code given below generates a random number between x and y-1 (both inclusive) using
the randrange function of the random module.
x=10
y=50
print(random.randrange(x,y))
MATH LIBRARY –
math is another useful module in Python. Once you have imported the math module, you can
use some of the below functions: