SlideShare a Scribd company logo
1 of 36
Download to read offline
Introduction to Python
Programming
Python
Printing Messages on Screen
Reading input from console
Python Identifiers and Keywords
Data types in Python
Operators
Using math library function
 Developed by GuidoVan Roussum in
1991.
 Python is processed at runtime by
the interpreter.
 There is no need to compile your
program before executing it.
 Python has a design philosophy that
emphasizes code readability.
Hello World Program
mohammed.sikander@cranessoftware.com 4
>>> print(‘Hello’)
>>> print(‘Welcome to Python Programming!')
Print Statement
#Python 2.x Code #Python 3.x Code
print "SIKANDER"
print 5
print 2 + 4
SIKANDER
5
6
The "print" directive prints contents to console.
 In Python 2, the "print" statement is not a function,and therefore it is invoked
without parentheses.
 In Python 3, it is a function, and must be invoked with parentheses.
(It includes a newline, unlike in C)
 A hash sign (#) that is not inside a string literal begins a comment.
All characters after the # and up to the end of the physical line are part of the
comment and the Python interpreter ignores them.
print("SIKANDER")
print(5)
print(2 + 4)
Output
Python Identifiers
 Identifier is the name given to entities like class, functions, variables etc.
 Rules for writing identifiers
 Identifiers can be a combination of letters in lowercase (a to z) or
uppercase (A to Z) or digits (0 to 9) or an underscore (_).
 An identifier cannot start with a digit.
 Keywords cannot be used as identifiers.
 We cannot use special symbols like !, @, #, $, % etc. in our identifier.
Python Keywords
 Keywords are the reserved words in Python.
 We cannot use a keyword as variable
name, function name or any other identifier.
 They are used to define the syntax and structure of the
Python language.
 keywords are case sensitive.
 There are 33 keywords in Python 3.3.
Keywords in Python programming language
False class finally is return
None continue for lambda try
True def from nonlocal while
and del global not with
as elif if or yield
assert else import pass
break except in raise
All the keywords except True, False and None are in
lowercase
PythonVariables
 A variable is a location in memory used to store
some data (value).
 They are given unique names to differentiate
between different memory locations.The rules
for writing a variable name is same as the rules
for writing identifiers in Python.
 We don't need to declare a variable before using
it.
 In Python, we simply assign a value to a variable
and it will exist.
 We don't even have to declare the type of the
variable.This is handled internally according to
the type of value we assign to the variable.
Variable assignment
 We use the assignment operator (=) to
assign values to a variable.
 Any type of value can be assigned to any
valid variable.
Multiple assignments
 In Python, multiple assignments can be made in
a single statement as follows:
If we want to assign the same value to multiple
variables at once, we can do this as
x = y = z = "same"
Data types in Python
 Every value in Python has a datatype.
 Since everything is an object in Python
 Data types are actually classes and variables are
instance (object) of these classes.
 Numbers
 int
 float
 String – str
 Boolean - bool
Type of object – type()
 type() function can be used to know to
which class a variable or a value belongs
to.
Reading Input
Python 2.x Python 3.x
 print("Enter the name :")
 name = raw_input()
 print(name)
 print("Enter the name :")
 name = input()
 print(name)
If you want to prompt the user for input, you can use raw_input in Python 2.X, and
just input in Python 3.
 name = input("Enter the name :")
 print(“Welcome “ + name)
 Input function can print a prompt and
read the input.
Arithmetic Operators
Operator Meaning Example
+ Add two operands or unary plus x + y
+2
- Subtract right operand from the left or
unary minus
x - y
-2
* Multiply two operands x * y
/ Divide left operand by the right one
(always results into float)
x / y
% Modulus - remainder of the division of left
operand by the right
x % y (remainder of x/y)
// Floor division - division that results into
whole number adjusted to the left in the
number line
x // y
** Exponent - left operand raised to the
power of right
x**y (x to the power y)
Arithmetic Operators
What is the output
 Data read from input is in the form of string.
 To convert to integer, we need to typecast.
 Syntax : datatype(object)
Exercise
 Write a program to calculate the area of
Circle given its radius.
 area = πr2
 Write a program to calculate the area of
circle given its 3 sides.
 s = (a+b+c)/2
 area = √(s(s-a)*(s-b)*(s-c))
Given the meal price (base cost of a meal), tip percent (the percentage of
the meal price being added as tip), and tax percent (the percentage of
the meal price being added as tax) for a meal, find and print the
meal's total cost.
Input
12.00 20 8
Output
The total meal cost is 15 dollars.
 Temperature Conversion.
 Given the temperature in Celsius, convert
it to Fahrenheit.
 Given the temperature in Fahrenheit,
convert it to Celsius.
Relational operators
 Relational operators are used to compare values.
 It returns True or False according to condition
Operator Meaning Example
>
Greater that - True if left operand is greater than the
right
x > y
< Less that - True if left operand is less than the right x < y
== Equal to - True if both operands are equal x == y
!= Not equal to - True if operands are not equal x != y
>=
Greater than or equal to - True if left operand is
greater than or equal to the right
x >= y
<=
Less than or equal to - True if left operand is less
than or equal to the right
x <= y
Logical operators
Operator Meaning Example
and True if both the operands are true x and y
or True if either of the operands is true x or y
not
True if operand is false
(complements the operand)
not x
Bitwise operators
 Bitwise operators operates on individual
bits of number.
Operator Meaning Example
& Bitwise AND 10 & 4 = 0
| Bitwise OR 10 | 4 = 14
~ Bitwise NOT ~10 = -11
^ Bitwise XOR 10 ^ 4 = 14
>> Bitwise right shift 10>> 2 = 2
<< Bitwise left shift 10<< 2 = 42
Compound Assignment operators
Operator Example Equivatent to
= x = 5 x = 5
+= x += 5 x = x + 5
-= x -= 5 x = x - 5
*= x *= 5 x = x * 5
/= x /= 5 x = x / 5
%= x %= 5 x = x % 5
//= x //= 5 x = x // 5
**= x **= 5 x = x ** 5
&= x &= 5 x = x & 5
|= x |= 5 x = x | 5
^= x ^= 5 x = x ^ 5
>>= x >>= 5 x = x >> 5
<<= x <<= 5 x = x << 5
Precedence of Python Operators
Operators Meaning
() Parentheses
** Exponent
+x, -x, ~x Unary plus, Unary minus, Bitwise NOT
*, /, //, % Multiplication,Division, Floor division, Modulus
+, - Addition,Subtraction
<<, >> Bitwise shift operators
& Bitwise AND
^ Bitwise XOR
| Bitwise OR
==, !=, >, >=, <, <=, is, is not, in, not in Comparisions,Identity, Membership operators
not Logical NOT
and Logical AND
or Logical OR

More Related Content

What's hot

What's hot (16)

Lecture 11 - Functions
Lecture 11 - FunctionsLecture 11 - Functions
Lecture 11 - Functions
 
Python unit 2 as per Anna university syllabus
Python unit 2 as per Anna university syllabusPython unit 2 as per Anna university syllabus
Python unit 2 as per Anna university syllabus
 
Lecture 14 - Scope Rules
Lecture 14 - Scope RulesLecture 14 - Scope Rules
Lecture 14 - Scope Rules
 
FLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHONFLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHON
 
Introduction to golang
Introduction to golangIntroduction to golang
Introduction to golang
 
Types of Statements in Python Programming Language
Types of Statements in Python Programming LanguageTypes of Statements in Python Programming Language
Types of Statements in Python Programming Language
 
Lecture 13 - Storage Classes
Lecture 13 - Storage ClassesLecture 13 - Storage Classes
Lecture 13 - Storage Classes
 
Python Objects
Python ObjectsPython Objects
Python Objects
 
Python - Control Structures
Python - Control StructuresPython - Control Structures
Python - Control Structures
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python Programming
 
Conditional Statements
Conditional StatementsConditional Statements
Conditional Statements
 
Function
FunctionFunction
Function
 
Control Structures: Part 1
Control Structures: Part 1Control Structures: Part 1
Control Structures: Part 1
 
Looping
LoopingLooping
Looping
 
C language presentation
C language presentationC language presentation
C language presentation
 
Lesson 02 python keywords and identifiers
Lesson 02   python keywords and identifiersLesson 02   python keywords and identifiers
Lesson 02 python keywords and identifiers
 

Similar to 1_Python Basics.pdf

basics of python programming5.pdf
basics of python programming5.pdfbasics of python programming5.pdf
basics of python programming5.pdfPushkaran3
 
Python_Unit-1_PPT_Data Types.pptx
Python_Unit-1_PPT_Data Types.pptxPython_Unit-1_PPT_Data Types.pptx
Python_Unit-1_PPT_Data Types.pptxSahajShrimal1
 
PE1 Module 2.ppt
PE1 Module 2.pptPE1 Module 2.ppt
PE1 Module 2.pptbalewayalew
 
Python-review1.ppt
Python-review1.pptPython-review1.ppt
Python-review1.pptjaba kumar
 
Python-review1.pdf
Python-review1.pdfPython-review1.pdf
Python-review1.pdfpaijitk
 
Basic Concepts in Python
Basic Concepts in PythonBasic Concepts in Python
Basic Concepts in PythonSumit Satam
 
TOPIC-2-Expression Variable Assignment Statement.pdf
TOPIC-2-Expression Variable Assignment Statement.pdfTOPIC-2-Expression Variable Assignment Statement.pdf
TOPIC-2-Expression Variable Assignment Statement.pdfEjazAlam23
 
1_1_python-course-notes-sections-1-7.pdf
1_1_python-course-notes-sections-1-7.pdf1_1_python-course-notes-sections-1-7.pdf
1_1_python-course-notes-sections-1-7.pdfJavier Crisostomo
 
Python programing
Python programingPython programing
Python programinghamzagame
 

Similar to 1_Python Basics.pdf (20)

basics of python programming5.pdf
basics of python programming5.pdfbasics of python programming5.pdf
basics of python programming5.pdf
 
Welcome to python workshop
Welcome to python workshopWelcome to python workshop
Welcome to python workshop
 
Python Operators
Python OperatorsPython Operators
Python Operators
 
Python basics
Python basicsPython basics
Python basics
 
Python_Unit-1_PPT_Data Types.pptx
Python_Unit-1_PPT_Data Types.pptxPython_Unit-1_PPT_Data Types.pptx
Python_Unit-1_PPT_Data Types.pptx
 
PE1 Module 2.ppt
PE1 Module 2.pptPE1 Module 2.ppt
PE1 Module 2.ppt
 
Python programming
Python  programmingPython  programming
Python programming
 
Python-review1.ppt
Python-review1.pptPython-review1.ppt
Python-review1.ppt
 
Python Lecture 2
Python Lecture 2Python Lecture 2
Python Lecture 2
 
Python-review1.pdf
Python-review1.pdfPython-review1.pdf
Python-review1.pdf
 
Python-review1.ppt
Python-review1.pptPython-review1.ppt
Python-review1.ppt
 
Basic Concepts in Python
Basic Concepts in PythonBasic Concepts in Python
Basic Concepts in Python
 
Python.pptx
Python.pptxPython.pptx
Python.pptx
 
pythonQuick.pdf
pythonQuick.pdfpythonQuick.pdf
pythonQuick.pdf
 
python notes.pdf
python notes.pdfpython notes.pdf
python notes.pdf
 
python 34💭.pdf
python 34💭.pdfpython 34💭.pdf
python 34💭.pdf
 
TOPIC-2-Expression Variable Assignment Statement.pdf
TOPIC-2-Expression Variable Assignment Statement.pdfTOPIC-2-Expression Variable Assignment Statement.pdf
TOPIC-2-Expression Variable Assignment Statement.pdf
 
Python.pptx
Python.pptxPython.pptx
Python.pptx
 
1_1_python-course-notes-sections-1-7.pdf
1_1_python-course-notes-sections-1-7.pdf1_1_python-course-notes-sections-1-7.pdf
1_1_python-course-notes-sections-1-7.pdf
 
Python programing
Python programingPython programing
Python programing
 

Recently uploaded

Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 

Recently uploaded (20)

Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 

1_Python Basics.pdf

  • 2. Python Printing Messages on Screen Reading input from console Python Identifiers and Keywords Data types in Python Operators Using math library function
  • 3.  Developed by GuidoVan Roussum in 1991.  Python is processed at runtime by the interpreter.  There is no need to compile your program before executing it.  Python has a design philosophy that emphasizes code readability.
  • 4. Hello World Program mohammed.sikander@cranessoftware.com 4 >>> print(‘Hello’) >>> print(‘Welcome to Python Programming!')
  • 5. Print Statement #Python 2.x Code #Python 3.x Code print "SIKANDER" print 5 print 2 + 4 SIKANDER 5 6 The "print" directive prints contents to console.  In Python 2, the "print" statement is not a function,and therefore it is invoked without parentheses.  In Python 3, it is a function, and must be invoked with parentheses. (It includes a newline, unlike in C)  A hash sign (#) that is not inside a string literal begins a comment. All characters after the # and up to the end of the physical line are part of the comment and the Python interpreter ignores them. print("SIKANDER") print(5) print(2 + 4) Output
  • 6. Python Identifiers  Identifier is the name given to entities like class, functions, variables etc.  Rules for writing identifiers  Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore (_).  An identifier cannot start with a digit.  Keywords cannot be used as identifiers.  We cannot use special symbols like !, @, #, $, % etc. in our identifier.
  • 7. Python Keywords  Keywords are the reserved words in Python.  We cannot use a keyword as variable name, function name or any other identifier.  They are used to define the syntax and structure of the Python language.  keywords are case sensitive.  There are 33 keywords in Python 3.3.
  • 8. Keywords in Python programming language False class finally is return None continue for lambda try True def from nonlocal while and del global not with as elif if or yield assert else import pass break except in raise All the keywords except True, False and None are in lowercase
  • 9. PythonVariables  A variable is a location in memory used to store some data (value).  They are given unique names to differentiate between different memory locations.The rules for writing a variable name is same as the rules for writing identifiers in Python.  We don't need to declare a variable before using it.  In Python, we simply assign a value to a variable and it will exist.  We don't even have to declare the type of the variable.This is handled internally according to the type of value we assign to the variable.
  • 10. Variable assignment  We use the assignment operator (=) to assign values to a variable.  Any type of value can be assigned to any valid variable.
  • 11. Multiple assignments  In Python, multiple assignments can be made in a single statement as follows: If we want to assign the same value to multiple variables at once, we can do this as x = y = z = "same"
  • 12. Data types in Python  Every value in Python has a datatype.  Since everything is an object in Python  Data types are actually classes and variables are instance (object) of these classes.  Numbers  int  float  String – str  Boolean - bool
  • 13. Type of object – type()  type() function can be used to know to which class a variable or a value belongs to.
  • 14. Reading Input Python 2.x Python 3.x  print("Enter the name :")  name = raw_input()  print(name)  print("Enter the name :")  name = input()  print(name) If you want to prompt the user for input, you can use raw_input in Python 2.X, and just input in Python 3.
  • 15.  name = input("Enter the name :")  print(“Welcome “ + name)  Input function can print a prompt and read the input.
  • 16. Arithmetic Operators Operator Meaning Example + Add two operands or unary plus x + y +2 - Subtract right operand from the left or unary minus x - y -2 * Multiply two operands x * y / Divide left operand by the right one (always results into float) x / y % Modulus - remainder of the division of left operand by the right x % y (remainder of x/y) // Floor division - division that results into whole number adjusted to the left in the number line x // y ** Exponent - left operand raised to the power of right x**y (x to the power y)
  • 18. What is the output
  • 19.  Data read from input is in the form of string.  To convert to integer, we need to typecast.  Syntax : datatype(object)
  • 20.
  • 21. Exercise  Write a program to calculate the area of Circle given its radius.  area = πr2
  • 22.
  • 23.
  • 24.  Write a program to calculate the area of circle given its 3 sides.  s = (a+b+c)/2  area = √(s(s-a)*(s-b)*(s-c))
  • 25.
  • 26. Given the meal price (base cost of a meal), tip percent (the percentage of the meal price being added as tip), and tax percent (the percentage of the meal price being added as tax) for a meal, find and print the meal's total cost. Input 12.00 20 8 Output The total meal cost is 15 dollars.
  • 27.
  • 28.  Temperature Conversion.  Given the temperature in Celsius, convert it to Fahrenheit.  Given the temperature in Fahrenheit, convert it to Celsius.
  • 29. Relational operators  Relational operators are used to compare values.  It returns True or False according to condition Operator Meaning Example > Greater that - True if left operand is greater than the right x > y < Less that - True if left operand is less than the right x < y == Equal to - True if both operands are equal x == y != Not equal to - True if operands are not equal x != y >= Greater than or equal to - True if left operand is greater than or equal to the right x >= y <= Less than or equal to - True if left operand is less than or equal to the right x <= y
  • 30.
  • 31.
  • 32. Logical operators Operator Meaning Example and True if both the operands are true x and y or True if either of the operands is true x or y not True if operand is false (complements the operand) not x
  • 33.
  • 34. Bitwise operators  Bitwise operators operates on individual bits of number. Operator Meaning Example & Bitwise AND 10 & 4 = 0 | Bitwise OR 10 | 4 = 14 ~ Bitwise NOT ~10 = -11 ^ Bitwise XOR 10 ^ 4 = 14 >> Bitwise right shift 10>> 2 = 2 << Bitwise left shift 10<< 2 = 42
  • 35. Compound Assignment operators Operator Example Equivatent to = x = 5 x = 5 += x += 5 x = x + 5 -= x -= 5 x = x - 5 *= x *= 5 x = x * 5 /= x /= 5 x = x / 5 %= x %= 5 x = x % 5 //= x //= 5 x = x // 5 **= x **= 5 x = x ** 5 &= x &= 5 x = x & 5 |= x |= 5 x = x | 5 ^= x ^= 5 x = x ^ 5 >>= x >>= 5 x = x >> 5 <<= x <<= 5 x = x << 5
  • 36. Precedence of Python Operators Operators Meaning () Parentheses ** Exponent +x, -x, ~x Unary plus, Unary minus, Bitwise NOT *, /, //, % Multiplication,Division, Floor division, Modulus +, - Addition,Subtraction <<, >> Bitwise shift operators & Bitwise AND ^ Bitwise XOR | Bitwise OR ==, !=, >, >=, <, <=, is, is not, in, not in Comparisions,Identity, Membership operators not Logical NOT and Logical AND or Logical OR