Python Notes Unit 1
Python Notes Unit 1
Definition
• Web Development
• Machine Learning and Artificial Intelligence
• Data Science
• Game Development
• Audio and Visual Application
• Software Development
Interpreter mode
• Interpreter mode refers to running Python scripts through a file. You write
the entire code in a Python file (usually with a .py extension) and then run it
using the Python interpreter.
• When you execute the above command, Python reads the script file from
top to bottom and runs it as a whole.
• Example:
python script.py
Interactive mode
• Interactive mode allows you to run Python commands individually in a
shell or a Python prompt. The Python shell is invoked by typing python
or python3 in your terminal or command line without specifying a
script.
• When you enter interactive mode, you can type Python expressions,
and the interpreter immediately executes them and gives feedback.
• Example –
$ python
>>> print("Hello, World!")
Hello, World!
DataTypes
Int, String,
Float, List, Dictionary Set Bool
Complex Tuples
Numeric
Code:
a=7
print("Type of a: ", type(a))
b = 7.0
print("\nType of b: ", type(b))
c = 2 + 4j
print("\nType of c: ", type(c))
Sequence (String)
Code:
String1 = 'Welcome to the Geeks
World’
print("String with the use of Single
Quotes: ")
print(String1)print(type(String1))
Sequence(List)
Code:
List = []
print("Initial blank List: ")
print(List)
List = ['GeeksForGeeks’]
print("\nList with the use of String: ")
print(List)List = ["Geeks", "For", "Geeks"]
print("\nList containing multiple values:
")
print(List[0])
print(List[2])
List = [['Geeks', 'For'], ['Geeks’]]
print("\nMulti-Dimensional List: ")
print(List)
Sequence (Tuples)
Code:
Tuple1 = ()
print("Initial empty Tuple: ")
print(Tuple1)Tuple1 = ('Geeks', 'For’)
print("\nTuple with the use of String:
")
print(Tuple1)
list1 = [1, 2, 4, 7, 6]
print("\nTuple using List: ")
print(tuple(list1))
Boolean
Code:
a = True
Print(type(a))
b = False
Print(type(b))
Set
Code:
set1 = set()
print("Initial blank Set: ")
print(set1)
set1 = set("GeeksForGeeks")
print("\nSet with the use of String: ")
print(set1)
set1 = set(["Geeks", "For", "Geeks"])
print("\nSet with the use of List: ")
print(set1)set1 = set([1, 2, 'Geeks', 4,
'For', 6, 'Geeks’])
print("\nSet with the use of Mixed
Values")
print(set1)
Dictionary
Code:
Dict = {}
print("Empty Dictionary: ")
print(Dict)
Dict = {1: 'Geeks', 2: 'For', 6: 'Geeks’}
print("\nDictionary with the use of Integer Keys: ")
print(Dict)
Dict = {'Name': 'Geeks', 1: [1, 2, 6, 4]}
print("\nDictionary with the use of Mixed Keys: ")
print(Dict)
Dict = dict({1: 'Geeks', 2: 'For', 6: 'Geeks’})
print("\nDictionary with the use of dict(): ")
Recap….
Types Description Example
Sequence Represents a collection of ordered and indexed value ‘str’, ‘list’, ‘tuple’
and sequence of Characters
Arithmetic Bitwise
Assignment Membership
Comparison Identity
Logical
Arithmetic Operators
Arithmetic operators are used with numeric values to perform common mathematical
operations:
Operator Name Example
+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus x%y
** Exponentiation x ** y
// Floor division x // y
Code:
x=7
y=3
print(x+y)
print(x-y)
print(x * y)
print(x/y)
print(x % y)
print(x**y)
print(x//y)
Precedence & Associativity
Operators Associativity
() Left to Right
** Right to Left
= x=7 x=7
+= x += 6 x=x+6
-= x -= 6 x=x-6
*= x *= 6 x=x*6
/= x /= 6 x=x/6
%= x %= 6 x=x%6
//= x //= 6 x = x // 6
**= x **= 6 x = x ** 6
Code:
a = 50
b = 10
print('a=b:', a==b)
print('a+=b:', a+b)
print('a-=b:', a-b)
print('a*=b:', a*b)
print('a%=b:', a%b)
print('a**=b:', a**b)
print('a/=b:', a/b)
Comparison operators
Comparison operators are used to compare two values:
== Equal x == y
!= Not equal x != y
not Reverse the result, returns False if the not(x < 5 and x < 10)
result is true
Code:
a=7
print('Is this statement
true?:',a > 3 and a < 7)
print('Any one statement is
true?:',a > 3 or a < 7)
print('Each statement is
true then return False and
vice-versa:',(not(a > 3 and a
< 7)))
Bitwise Operators
Bitwise operators are used to compare (binary) numbers:
elif expression 2:
# block of statements
elif expression 3:
# block of statements
else:
# block of statements
Code:
Whether the student fails or pass
marks = int(input("Enter the marks? "))