SlideShare a Scribd company logo
Python - The Basics
M.BOBBY
ASSISTANT PROFESSOR & HEAD,
DEPARTMENT OF COMPUTER SCIENCE
SRI ADI CHUNCHANAGIRI WOMEN’S COLLEGE, CUMBUM
A Code Sample (in IDLE)
x = 34 - 23 # A comment.
y = “Hello” # Another one.
z = 3.45
if z == 3.45 or y == “Hello”:
x = x + 1
y = y + “ World” # String concat.
print x
print y
Enough to Understand the Code
• Indentation matters to code meaning
• Block structure indicated by indentation
• First assignment to a variable creates it
• Variable types don’t need to be declared.
• Python figures out the variable types on its own.
• Assignment is = and comparison is ==
• For numbers + - * / % are as expected
• Special use of + for string concatenation and % for string formatting (as in C’s
printf)
• Logical operators are words (and, or, not) not symbols
• The basic printing command is print
Basic Datatypes
• Integers (default for numbers)
z = 5 / 2 # Answer 2, integer division
• Floats
x = 3.456
• Strings
• Can use “” or ‘’ to specify with “abc” == ‘abc’
• Unmatched can occur within the string: “matt’s”
• Use triple double-quotes for multi-line strings or strings than contain
both ‘ and “ inside of them:
“““a‘b“c”””
Whitespace
Whitespace is meaningful in Python: especially indentation and placement of
newlines
•Use a newline to end a line of code
Use  when must go to next line prematurely
•No braces {} to mark blocks of code, use consistent indentation instead
• First line with less indentation is outside of the block
• First line with more indentation starts a nested block
•Colons start of a new block in many constructs, e.g. function definitions, then
clauses
Comments
• Start comments with #, rest of line is ignored
• Can include a “documentation string” as the first line of a new function or class
you define
• Development environments, debugger, and other tools use it: it’s good style to
include one
def fact(n):
“““fact(n) assumes n is a positive integer and returns
facorial of n.”””
assert(n>0)
return 1 if n==1 else n*fact(n-1)
Assignment
• Binding a variable in Python means setting a name to hold a reference to some object
• Assignment creates references, not copies
• Names in Python do not have an intrinsic type, objects have types
• Python determines the type of the reference automatically based on what data is assigned to it
• You create a name the first time it appears on the left side of an assignment expression:
x = 3
• A reference is deleted via garbage collection after any names bound to it have passed out of scope
• Python uses reference semantics (more later)
Naming Rules
• Names are case sensitive and cannot start with a number. They can contain
letters, numbers, and underscores.
bob Bob _bob _2_bob_ bob_2 BoB
• There are some reserved words:
and, assert, break, class, continue, def, del, elif,
else, except, exec, finally, for, from, global, if,
import, in, is, lambda, not, or, pass, print, raise,
return, try, while
Naming conventions
The Python community has these recommend-ed naming conventions
•joined_lower for functions, methods and, attributes
•joined_lower or ALL_CAPS for constants
•StudlyCaps for classes
•camelCase only to conform to pre-existing conventions
•Attributes: interface, _internal, __private
Assignment
• You can assign to multiple names at the same time
>>> x, y = 2, 3
>>> x
2
>>> y
3
This makes it easy to swap values
>>> x, y = y, x
• Assignments can be chained
>>> a = b = x = 2
Accessing Non-Existent Name
Accessing a name before it’s been properly created (by placing it on the left side of an assignment),
raises an error
>>> y
Traceback (most recent call last):
File "<pyshell#16>", line 1, in -toplevel-
y
NameError: name ‘y' is not defined
>>> y = 3
>>> y
3

More Related Content

What's hot (20)

PDF
Python
대갑 김
 
PPTX
Python ppt
Anush verma
 
PDF
Python Basics
tusharpanda88
 
PPT
Introduction to Python - Part Two
amiable_indian
 
PDF
Python-02| Input, Output & Import
Mohd Sajjad
 
PDF
Python revision tour II
Mr. Vikram Singh Slathia
 
PPTX
Introduction to Python Programming
VijaySharma802
 
PPTX
Iterarators and generators in python
Sarfaraz Ghanta
 
PPTX
Python language data types
Hoang Nguyen
 
PDF
Python basic
Saifuddin Kaijar
 
PPTX
An Introduction : Python
Raghu Kumar
 
PPTX
Programming in Python
Tiji Thomas
 
PPT
Introduction to Python - Part Three
amiable_indian
 
PPT
python.ppt
shreyas_test_1234
 
PPTX
Python Basics
Adheetha O. V
 
PDF
python codes
tusharpanda88
 
PPTX
Python basics
Hoang Nguyen
 
PPTX
Introduction to the basics of Python programming (part 1)
Pedro Rodrigues
 
PPTX
Python programming language
Ebrahim Shakhatreh
 
PPTX
Learn Python The Hard Way Presentation
Amira ElSharkawy
 
Python
대갑 김
 
Python ppt
Anush verma
 
Python Basics
tusharpanda88
 
Introduction to Python - Part Two
amiable_indian
 
Python-02| Input, Output & Import
Mohd Sajjad
 
Python revision tour II
Mr. Vikram Singh Slathia
 
Introduction to Python Programming
VijaySharma802
 
Iterarators and generators in python
Sarfaraz Ghanta
 
Python language data types
Hoang Nguyen
 
Python basic
Saifuddin Kaijar
 
An Introduction : Python
Raghu Kumar
 
Programming in Python
Tiji Thomas
 
Introduction to Python - Part Three
amiable_indian
 
python.ppt
shreyas_test_1234
 
Python Basics
Adheetha O. V
 
python codes
tusharpanda88
 
Python basics
Hoang Nguyen
 
Introduction to the basics of Python programming (part 1)
Pedro Rodrigues
 
Python programming language
Ebrahim Shakhatreh
 
Learn Python The Hard Way Presentation
Amira ElSharkawy
 

Similar to Python The basics (20)

PPT
Python ppt
Mohita Pandey
 
PPT
Python for Engineers and Architects Stud
RaviRamachandraR
 
PPTX
manish python.pptx
ssuser92d141
 
PPT
pysdasdasdsadsadsadsadsadsadasdasdthon1.ppt
kashifmajeedjanjua
 
PPT
Lenguaje Python
RalAnteloJurado
 
PPT
python1.ppt
JemuelPinongcos1
 
PPT
python1.ppt
ALOK52916
 
PPT
Python Basics
MobeenAhmed25
 
PPT
python1.ppt
AshokRachapalli1
 
PPT
python1.ppt
VishwasKumar58
 
PPT
python1.ppt
RedenOriola
 
PPT
python1.ppt
RajPurohit33
 
PPT
coolstuff.ppt
GeorgePama1
 
PPT
Introductio_to_python_progamming_ppt.ppt
HiralPatel798996
 
PPT
python1.ppt
SATHYANARAYANAKB
 
PPT
Learn Python in Three Hours - Presentation
Naseer-ul-Hassan Rehman
 
PPT
Kavitha_python.ppt
KavithaMuralidharan2
 
PPT
python1.pptpppppppppppppppppppppppppppppppp
divijareddy0502
 
PPT
Learn Python in three hours - Python is an experiment
Anil Yadav
 
PPT
python programing 101 presentation ... Let's start
Mohsen Hefni
 
Python ppt
Mohita Pandey
 
Python for Engineers and Architects Stud
RaviRamachandraR
 
manish python.pptx
ssuser92d141
 
pysdasdasdsadsadsadsadsadsadasdasdthon1.ppt
kashifmajeedjanjua
 
Lenguaje Python
RalAnteloJurado
 
python1.ppt
JemuelPinongcos1
 
python1.ppt
ALOK52916
 
Python Basics
MobeenAhmed25
 
python1.ppt
AshokRachapalli1
 
python1.ppt
VishwasKumar58
 
python1.ppt
RedenOriola
 
python1.ppt
RajPurohit33
 
coolstuff.ppt
GeorgePama1
 
Introductio_to_python_progamming_ppt.ppt
HiralPatel798996
 
python1.ppt
SATHYANARAYANAKB
 
Learn Python in Three Hours - Presentation
Naseer-ul-Hassan Rehman
 
Kavitha_python.ppt
KavithaMuralidharan2
 
python1.pptpppppppppppppppppppppppppppppppp
divijareddy0502
 
Learn Python in three hours - Python is an experiment
Anil Yadav
 
python programing 101 presentation ... Let's start
Mohsen Hefni
 
Ad

More from Bobby Murugesan (10)

PDF
Study Material for Problem Solving Techniques
Bobby Murugesan
 
PDF
Fundamentals of Information Technology study Material
Bobby Murugesan
 
PDF
STUDY MATERIA FOR "THE VIOLENCE AGAINST WOMEN "
Bobby Murugesan
 
PDF
NOTES FOR CYBER SECURITY INITIATIVES BY THE INDIAN GOVERNMENT
Bobby Murugesan
 
PDF
Computer skills for web designing and video editing_Lab Manual.pdf
Bobby Murugesan
 
PDF
Python Lab Manual
Bobby Murugesan
 
PPTX
Sequence Types in Python Programming
Bobby Murugesan
 
PPT
Impressive Google Apps
Bobby Murugesan
 
PDF
How to register in Swayam
Bobby Murugesan
 
PPTX
Green computing introduction
Bobby Murugesan
 
Study Material for Problem Solving Techniques
Bobby Murugesan
 
Fundamentals of Information Technology study Material
Bobby Murugesan
 
STUDY MATERIA FOR "THE VIOLENCE AGAINST WOMEN "
Bobby Murugesan
 
NOTES FOR CYBER SECURITY INITIATIVES BY THE INDIAN GOVERNMENT
Bobby Murugesan
 
Computer skills for web designing and video editing_Lab Manual.pdf
Bobby Murugesan
 
Python Lab Manual
Bobby Murugesan
 
Sequence Types in Python Programming
Bobby Murugesan
 
Impressive Google Apps
Bobby Murugesan
 
How to register in Swayam
Bobby Murugesan
 
Green computing introduction
Bobby Murugesan
 
Ad

Recently uploaded (20)

PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PPTX
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PPTX
How to Set Maximum Difference Odoo 18 POS
Celine George
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PDF
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
How to Set Maximum Difference Odoo 18 POS
Celine George
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 

Python The basics

  • 1. Python - The Basics M.BOBBY ASSISTANT PROFESSOR & HEAD, DEPARTMENT OF COMPUTER SCIENCE SRI ADI CHUNCHANAGIRI WOMEN’S COLLEGE, CUMBUM
  • 2. A Code Sample (in IDLE) x = 34 - 23 # A comment. y = “Hello” # Another one. z = 3.45 if z == 3.45 or y == “Hello”: x = x + 1 y = y + “ World” # String concat. print x print y
  • 3. Enough to Understand the Code • Indentation matters to code meaning • Block structure indicated by indentation • First assignment to a variable creates it • Variable types don’t need to be declared. • Python figures out the variable types on its own. • Assignment is = and comparison is == • For numbers + - * / % are as expected • Special use of + for string concatenation and % for string formatting (as in C’s printf) • Logical operators are words (and, or, not) not symbols • The basic printing command is print
  • 4. Basic Datatypes • Integers (default for numbers) z = 5 / 2 # Answer 2, integer division • Floats x = 3.456 • Strings • Can use “” or ‘’ to specify with “abc” == ‘abc’ • Unmatched can occur within the string: “matt’s” • Use triple double-quotes for multi-line strings or strings than contain both ‘ and “ inside of them: “““a‘b“c”””
  • 5. Whitespace Whitespace is meaningful in Python: especially indentation and placement of newlines •Use a newline to end a line of code Use when must go to next line prematurely •No braces {} to mark blocks of code, use consistent indentation instead • First line with less indentation is outside of the block • First line with more indentation starts a nested block •Colons start of a new block in many constructs, e.g. function definitions, then clauses
  • 6. Comments • Start comments with #, rest of line is ignored • Can include a “documentation string” as the first line of a new function or class you define • Development environments, debugger, and other tools use it: it’s good style to include one def fact(n): “““fact(n) assumes n is a positive integer and returns facorial of n.””” assert(n>0) return 1 if n==1 else n*fact(n-1)
  • 7. Assignment • Binding a variable in Python means setting a name to hold a reference to some object • Assignment creates references, not copies • Names in Python do not have an intrinsic type, objects have types • Python determines the type of the reference automatically based on what data is assigned to it • You create a name the first time it appears on the left side of an assignment expression: x = 3 • A reference is deleted via garbage collection after any names bound to it have passed out of scope • Python uses reference semantics (more later)
  • 8. Naming Rules • Names are case sensitive and cannot start with a number. They can contain letters, numbers, and underscores. bob Bob _bob _2_bob_ bob_2 BoB • There are some reserved words: and, assert, break, class, continue, def, del, elif, else, except, exec, finally, for, from, global, if, import, in, is, lambda, not, or, pass, print, raise, return, try, while
  • 9. Naming conventions The Python community has these recommend-ed naming conventions •joined_lower for functions, methods and, attributes •joined_lower or ALL_CAPS for constants •StudlyCaps for classes •camelCase only to conform to pre-existing conventions •Attributes: interface, _internal, __private
  • 10. Assignment • You can assign to multiple names at the same time >>> x, y = 2, 3 >>> x 2 >>> y 3 This makes it easy to swap values >>> x, y = y, x • Assignments can be chained >>> a = b = x = 2
  • 11. Accessing Non-Existent Name Accessing a name before it’s been properly created (by placing it on the left side of an assignment), raises an error >>> y Traceback (most recent call last): File "<pyshell#16>", line 1, in -toplevel- y NameError: name ‘y' is not defined >>> y = 3 >>> y 3