GE3151 Python
GE3151 Python
1.Outline the logic to swap the values of two identifies without using third variable ?
Examples
Scalar objects: Scalar objects are indivisible object that do not have internal structure.
string name=”python”
name=’python’
4.State about logical operators available in python with examples.
• Logical Operators are used to perform logical operations like and, or and not.
16 marks :
Number
• Numbers are created by numeric literals.
• Numeric objects are immutable, which means when an object is created its value cannot be
changed.
• Python will automatically convert a number from one type to another if it needs.
Python has three distinct numeric types:
1. Integers
• Boolean
2. Floating point numbers
3. Complex numbers.
Integers
• Integers represent negative and positive integers without fractional parts.
>>> type(2)
<class 'int'>
Boolean
• Boolean type is a subtype of plain integers.
• The simplest built-in type in Python is the bool type, it represents the truth
values,(True or False)
>>> type(True)
<class 'bool'>
>>> type(False)
<class 'bool'>
• Floating point numbers represents negative and positive numbers with fractional
Parts
>>> type(42.0)
<class 'float'>
Complex numbers
• A complex number is a number of the form A+Bi where i is the imaginary number.
Complex numbers have a real and imaginary part.
• Python supports complex numbers either by specifying the number in (real + imagj)
or (real + imagJ) form or using a built-in method complex(x, y).
>>> type(1+2j)
<class ‘complex’>
String
• A string type object is a sequence of characters.
• Strings start and end with single or double quotes.
• Python strings are immutable ie., once a string is generated, character within the string
cannot be modified.
List
• A list is a container which holds comma-separated values (items or elements)
between square brackets where all the items or elements need not to have the same type.
• A list without any element is called an empty list.
• Python lists are mutable.
Tuple
>>>a=(1,'python')
>>>type(a)
<class 'tuple'>
Dictionary
• Dictionary is an unordered collection of items with key: value pairs.
• In a dictionary,elements can be of any type.
expressions.
◦ P – Parentheses
• Parentheses have the highest precedence and parentheses are evaluated first.
• Examples:
o 2 * (3-1) = 4
o (1+1)**(5-2) = 8.
◦ E – Exponentiation
• Multiplication and Division have higher precedence than Addition and Subtraction.
• Examples:
o 2*3-1 = 5
o 6+4/2 = 8.
• Addition and Subtraction have lower precedence than Multiplication and Division
• The upper operator holds higher precedence than the lower operator.
Example Program for operator precedence:
colour = "red"
quantity = 0
else:
Output:
• When two operators have the same precedence, associativity helps to determine the
order of operations.
• Almost all the operators have left to right associativity except exponentiation.
Example:
• Hence if both the operators were present in an expression the left one will be evaluated first.
print(10*2//3)