0% found this document useful (0 votes)
40 views37 pages

Basics

Here is the Python program to solve the given problem: mileage = 20 amount_per_litre = 80 distance = 200 total_fuel_required = distance/mileage total_amount = total_fuel_required * amount_per_litre amount_each = total_amount/4 print("Amount each person needs to pay:",amount_each) if amount_each%5==0: print("True") else: print("False") This program takes the mileage of the vehicle, amount per litre of fuel and one way distance as input. It then calculates the total fuel required for complete journey, total amount for fuel and amount each person needs

Uploaded by

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

Basics

Here is the Python program to solve the given problem: mileage = 20 amount_per_litre = 80 distance = 200 total_fuel_required = distance/mileage total_amount = total_fuel_required * amount_per_litre amount_each = total_amount/4 print("Amount each person needs to pay:",amount_each) if amount_each%5==0: print("True") else: print("False") This program takes the mileage of the vehicle, amount per litre of fuel and one way distance as input. It then calculates the total fuel required for complete journey, total amount for fuel and amount each person needs

Uploaded by

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

Basics

CONTENTS

• Python Basics
– Comments
– Print
– User Input
– Variables
– Datatypes
– Operators
Python Basics-Comments
1. Comments are used for documenting the code .
2. Comment lines will not be executed by the python interpreter.

Multiline statements:
result=(2*5)+
Single Line Comment:
9
#This is a single line comment
result=(2+3)*(7+8)\
*(9+10)
Multi-line Comment:
result=(2*4*
“”” This is multiple ”””
4)
(Acceptable for all kinds of
brackets)
Python Basics-Print statement
To display output on the screen of the user
Python Basics-User Input statement
To get user input through keyboard
All inputs got from the user are treated as Strings
Python –Fundamental Components

Operators Identifiers

Variables Datatypes
Identifiers Variables

Case-Sensitive Eg : Bill_id
Cannot match keywords _billid1
Variables and its dimensions
Identify the
variable

Determine type , memory


Specify where it
required and operations that can
can be accessed
be performed on it

Specify how long it will Specify where it


be stored in memory is stored

Data held by
the variable
Datatypes
Built-in functions
For handling different types of user
inputs
int()-returns a integer number
float()-returns a decimal/floating point number
bool()-returns a Boolean value
complex()-returns a complex number
str()-returns a string
Simple Python codes
Operators
Arithmetic Operators
• An arithmetic operator is a mathematical operator that takes
two operands and performs a calculation on them. They are
used for simple arithmetic.
Arithmetic Operators
#Demo Program to test Arithmetic Operators
a=100
b=10
print ("The Sum = ",a+b)
print ("The Difference = ",a-b)
print ("The Product = ",a*b)
print ("The Quotient = ",a/b)
print ("The Remainder = ",a%30)
print ("The Exponent = ",a**2)
print ("The Floor Division =",a//30)
#Program End
Output:
The Sum = 110
The Difference = 90
The Product = 1000
The Quotient = 10.0
The Remainder = 10
The Exponent = 10000
The Floor Division = 3
Relational or Comparative operators
• A Relational operator is also called as Comparative operator
which checks the relationship between two operands. If the
relation is true, it returns True; otherwise it returns False
Relational Operators
#Demo Program to test Relational Operators
a=int (input("Enter a Value for A:"))
b=int (input("Enter a Value for B:"))
print ("A = ",a," and B = ",b)
print ("The a==b = ",a==b)
print ("The a > b = ",a>b)
print ("The a < b = ",a<b)
print ("The a >= b = ",a>=b)
print ("The a <= b = ",a<=0)
print ("The a != b = ",a!=b)
#Program End
Output:
Enter a Value for A:35
Enter a Value for B:56
A = 35 and B = 56
The a==b = False
The a > b = False
The a < b = True
The a >= b = False
The a <= b = False
The a != b = True
Logical operators
• In python, Logical operators are used to perform
logical operations on the given relational
expressions. There are three logical operators they
are and, or and not
Logical Operators

If A=(m>200), B=(m>300)
#Demo Program to test Logical Operators
a=int (input("Enter a Value for A:"))
b=int (input("Enter a Value for B:"))
print ("A = ",a, " and b = ",b)
print ("The a > b or a == b = ",a>b or a==b)
print ("The a > b and a == b = ",a>b and a==b)
print ("The not a > b = ",not a>b)
#Program End
Enter a Value for A:50
Enter a Value for B:40
A = 50 and b = 40
The a > b or a == b = True
The a > b and a == b = False
The not a > b = False
ASSIGNMENT OPERATORS
• In Python, = is a simple assignment operator to assign
values to variable. Let a = 5 and b = 10 assigns the value 5
to a and 10 to b these two assignment statement can
also be given as a,b=5,10 that assigns the value 5 and 10
on the right to the variables a and b respectively.
• There are various compound operators in Python like +=,
-=, *=, /=, %=, **= and //= are also available.
Assignment Operators
#Demo Program to test Assignment Operators
x=int (input("Type a Value for X : "))
print ("X = ",x)
print ("The x is =",x)
x+=20
print ("The x += 20 is =",x)
x-=5
print ("The x -= 5 is = ",x)
x*=5
print ("The x *= 5 is = ",x)
x/=2
print ("The x /= 2 is = ",x)
x%=3
print ("The x %= 3 is = ",x)
x**=2
print ("The x **= 2 is = ",x)
x//=3
print ("The x //= 3 is = ",x)
#Program End
Type a Value for X : 10
X = 10
The x is = 10
The x += 20 is = 30
The x -= 5 is = 25
The x *= 5 is = 125
The x /= 2 is = 62.5
The x %= 3 is = 2.5
The x **= 2 is = 6.25
The x //= 3 is = 2.0
Bitwise Operators
Shifts

We can also use a formula to find the answer, We can also use a formula to find the answer,
x<<y=x*2y x>>y=x/2y
Membership and Identity Operators
Coding Standards in python
Formatting output
• There are several ways to present the output of a program, data can be
printed in a human-readable form, or written to a file for future use.
Sometimes user often wants more control the formatting of output than
simply printing space-separated values. There are several ways to format
output.
• To use formatted string literals, begin a string with f or F before the opening quotation mark or
triple quotation mark.

• The str.format()method of strings help a user to get a fancier Output


Simple Python codes
Jack and his three friends have decided to go for a trip by sharing the expenses of the fuel equally .
Write a Python program to calculate the amount (in Rs) each of them need to put in for the complete
(both to and fro) journey. The program should also display True, if the amount to be paid by each
person is divisible by 5, otherwise it should display False. (Hint: Use the relational operators in print
statement.

Assume that mileage of the vehicle, amount per litre of fuel and distance for one way are given.

You might also like