Python Basics
Python Basics
1. Machine language
2. Assembly language
3. High-level language
Compiler & Interpreter
• These are the programs that execute instructions written in a high-
level language.
Compiler Interpreter
A unique program that It translates high-level instructions into an
processes statements intermediate form, which it then executes.
written in a particular
programming language
called source code and
converts them into
machine language or
“machine code” that a
computer’s processor
uses.
Binary Computation
Redix/Base
What is Python?
Python is a popular programming language. It was created by Guido van Rossum,
and released in 1991.
EXERCISE …
Python File Handling
import os
os.getcwd() #prints current working directory
os.chdir(‘path’) #change working directory
#'E:\\personal\\HRC\\Hansraj_College\\2022-23\\1st_year\\SEC'
Text files
f = open(“file.txt", "x")
"x" - Create - will create a file, returns an error if the file exist
"a" - Append - will create a file if the specified file does not exist
"w" - Write - will create a file if the specified file does not exist
#open and read the file after the
appending:
f = open("demofile2.txt", "r")
print(f.read())
Python operators
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Identity operators
• Membership operators
• Bitwise operators
Python operators
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Identity operators
• Membership operators
• Bitwise operators
Python operators
Arithmetic operators
Python operators
Arithmetic operators
Floor Division (//)
>>> 5//3
1
>>> -5//3
-2
>>> 5//-3
-2
>>> -5//-3
1
>>>
Python operators
Arithmetic operators
Floor Division (//)
>>> 5//3
1
>>> -5//3
-2
>>> 5//-3 FD Of Negative Tends to Approach -∞
-2
>>> -5//-3
1
>>>
Python operators
Arithmetic operators
Floor Division (//) Modulus (%)
>>> 5//3 >>> 5%3
1 2
>>> -5//3 >>> -5%3
-2 1
>>> 5//-3 >>> 5%-3
-2 -1
>>> -5//-3 >>> -5%-3
1 -2
>>> >>>
Python operators
Arithmetic operators
Floor Division (//) Modulus (%)
>>> 5//3 >>> 5%3
1 2
>>> -5//3 >>> -5%3
1 • -5//3= 2
-2
>>> 5%-3 • -5=6
>>> 5//-3
-1 • Difference is 1
-2
>>> -5%-3 (with the sign of divisor)
>>> -5//-3
1 -2
>>> >>>
Python operators
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Identity operators
• Membership operators
• Bitwise operators
Python operators
Assignment operators
Python operators
Assignment operators
Python operators
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Identity operators
• Membership operators
• Bitwise operators
Python operators
Comparison operators
Python operators
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Identity operators
• Membership operators
• Bitwise operators
Python operators
Logical operators
Python operators
Logical operators
OR AND NOT
A B Outcome A B Outcome A B
FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
FALSE TRUE TRUE FALSE TRUE FALSE TRUE FALSE
TRUE FALSE TRUE TRUE FALSE FALSE
TRUE TRUE TRUE TRUE TRUE TRUE
Python operators
Logical operators
OR AND NOT
A B Outcome A B Outcome A B
FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
FALSE TRUE TRUE FALSE TRUE FALSE TRUE FALSE
TRUE FALSE TRUE TRUE FALSE FALSE
TRUE TRUE TRUE TRUE TRUE TRUE
x=[1,2,3,4,5,6,7,8,9,0]
num=int(input('enter a number: '))
if num in x:
print ('the entered number', num, 'is in x')
else:
print ('the entered number', num, 'is NOT in x')
Python operators
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Identity operators
• Membership operators
• Bitwise operators
Python operators
Bitwise operators
Python operators
# Python program to show
# bitwise operators
Bitwise operators
a = 10 #1010 (Binary)
b = 4 #0100 (Binary)
print("Value of x:", x)
print("Value of y:", y)