0% found this document useful (0 votes)
5 views

Python Basics

The document provides an overview of Python programming, including its definition, capabilities, and advantages. It covers basic programming concepts, types of programming languages, and the functions of compilers and interpreters. Additionally, it explains Python's file handling, operators, and includes examples of various operator types.

Uploaded by

adityashahi2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Python Basics

The document provides an overview of Python programming, including its definition, capabilities, and advantages. It covers basic programming concepts, types of programming languages, and the functions of compilers and interpreters. Additionally, it explains Python's file handling, operators, and includes examples of various operator types.

Uploaded by

adityashahi2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Programming with Python

Basic Principles of Computer


Basic Principles of Computer
Computer and Programming Language
• Just we use any language to communicate with each other, we use
a programming language to communicate with the computer.

• Programming is a way to instruct the computer to perform various


tasks.

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

Has two independent symbols 0 and 1

Redix/Base
What is Python?
Python is a popular programming language. It was created by Guido van Rossum,
and released in 1991.

Python is a high-level language and it makes programming super


easy!
It is used for:
•web development (server-side),
•software development,
•mathematics,
•system scripting.
What can Python do?
•Python can be used on a server to create web
applications.
•Python can be used alongside software to create
workflows.
•Python can connect to database systems. It can
also read and modify files.
•Python can be used to handle big data and perform
complex mathematics.
•Python can be used for rapid prototyping, or for
production-ready software development.
Why Python?
•Python works on different platforms (Windows, Mac,
Linux, Raspberry Pi, etc).
•Python has a simple syntax similar to the English
language.
•Python has syntax that allows developers to write
programs with fewer lines than some other
programming languages.
•Python runs on an interpreter system, meaning that
code can be executed as soon as it is written. This
means that prototyping can be very quick.
•Python can be treated in a procedural way, an object-
oriented way or a functional way.
REVIEW…
Today…

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=int(input('enter a number: '))


if(x>=0 and x <10):
print('positive num, less than 10')
Python operators
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Identity operators
• Membership operators
• Bitwise operators
Python operators
Identity operators

x=int(input('enter a number: '))


y=int(input('enter a number: '))
if x is y:
print(x,'and', y, 'are equal')
elif x is not y:
print(x,'and', y, 'are NOT equal')
Python operators
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Identity operators
• Membership operators
• Bitwise operators
Python operators
Membership operators

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 bitwise AND operation


print("a & b =", a & b)
a&b=0
# Print bitwise OR operation
a | b = 14 print("a | b =", a | b)

# Print bitwise NOT operation


~a = -11 print("~a =", ~a) #~a is basically 2's complement of a

# print bitwise XOR operation


a ^ b = 14 print("a ^ b =", a ^ b)
Python operators
# Python program to show
Bitwise operators # shift operators

a = 10 #0000 1010 (Binary)


b = -10 #1111 0110 (Binary)

# print bitwise right shift operator


a >> 1 = 5 print("a >> 1 =", a >> 1) #0000 0101 = 5
print("b >> 1 =", b >> 1) #1111 1011 = -5
b >> 1 = -5
a = 5 #0000 0101 (Binary)
b = -10 #1111 0110 (Binary)

# print bitwise left shift operator


a << 1 = 10 print("a << 1 =", a << 1) #0000 1010 = 10
print("b << 1 =", b << 1) #1110 1100 = -20
b << 1 = -20
EXERCISE
• Taking input from the user with the input() function
• Indentation error
• Practice Python operators

# swapping of two variables


• Indentation error
x = 10
y = 50

# Swapping of two variables


# Using third variable
temp = x
x = y
y = temp

print("Value of x:", x)
print("Value of y:", y)

You might also like