SlideShare a Scribd company logo
Python



         -Mayur S. Mohite
Many languages require you to compile (translate) your program into a form that
the machine understands.

                         compile                    execute
       source code               byte code                 output
       Hello.java                Hello.class




Python is instead directly interpreted into machine instructions.
                         interpret
       source code               output
       Hello.py
Storing Values
Variable : A named piece of memory that can store a value.

Usage : Compute an expression's result, store that result into a variable
and use that variable later in the program.

Assignment statement : Stores a value into a variable.
Syntax :
                var_name = value

Examples :         x=5

A variable that has been given a value can be used in expressions.
Eg. x + 4 is 9
Printing Values

Print : Produces text output on the console.

Syntax :
          print "Message"
          print Expression
Prints the given text message or expression value on the console, and moves
the cursor down to the next line.

Examples :
       print "Hello, world!"
       age = 45
       print "You have", 65 - age, "years until retirement"

Output :
           Hello, world!
           You have 20 years until retirement
Input

input : Reads a number from user input.
raw_input : Reads a string of text from user input.

Example :
       age = input("How old are you? ")
       print "Your age is", age

         Output:
         How old are you? 53
         Your age is 53
       name = raw_input("Howdy, pardner. What's yer name?")
       print name, "... what a silly name!"
       Output:
       Howdy, pardner. What's yer name? Paris Hilton
       Paris Hilton ... what a silly name!
Loops

for loop : Repeats a set of statements over a group of values.

Example:
           for x in range(1, 6):
             print x, "squared is", x * x

if statement : Executes a group of statements only if a certain condition is true.
Otherwise, the statements are skipped.

Example:
           if gpa > 2.0:
              print "Your application is accepted."
if/else statement : Executes one block of statements if a certain
condition is True, and a second block of statements if it is False.

Example:
           if gpa > 2.0:
              print "Welcome to Mars University!"
           else:
              print "Your application is denied."

while loop : Executes a group of statements as long as a condition is True.
good for indefinite loops (repeat an unknown number of times)

Example:
           number = 1
           while number < 200:
             print number,
             number = number * 2
String

String : A sequence of text characters in a program.
Strings start and end with quotation mark " or apostrophe ' characters.
Examples:

"hello"
"This is a string"
"This, too, is a string. It can be very long!"

A string may not span across multiple lines or contain a " character.
"This is not
a legal String."
          "This is not a "legal" String either."
File Handling
Many programs handle data, which often comes from files.

Reading the entire contents of a file:
variableName = open("filename").read()

Example:
file_text = open("bankaccount.txt").read()

Reading a file line-by-line:

for line in open("filename").readlines():
  statements

Example:
count = 0
for line in open("bankaccount.txt").readlines():
  count = count + 1
print "The file contains", count, "lines."
Classes And Objects

A class acts like a container for all the methods and variable

A class definition looks like this:
class Point:
pass

Creating a new instance is called instantiation. To instantiate a
Point object, we call a function named Point:

blank = Point()

We can add new data to an instance using dot notation:
>>> blank.x = 3.0
>>> blank.y = 4.0

More Related Content

What's hot (8)

PPTX
PHP Powerpoint -- Teach PHP with this
Ian Macali
 
PDF
PHP7. Game Changer.
Haim Michael
 
PPTX
PHP Training Part1
than sare
 
PPT
07slide
Aboudi Sabbah
 
PDF
Strings IN C
yndaravind
 
PPTX
Php intro by sami kz
sami2244
 
PPTX
Lesson 4 constant
MLG College of Learning, Inc
 
ODP
PHP Web Programming
Muthuselvam RS
 
PHP Powerpoint -- Teach PHP with this
Ian Macali
 
PHP7. Game Changer.
Haim Michael
 
PHP Training Part1
than sare
 
07slide
Aboudi Sabbah
 
Strings IN C
yndaravind
 
Php intro by sami kz
sami2244
 
Lesson 4 constant
MLG College of Learning, Inc
 
PHP Web Programming
Muthuselvam RS
 

Viewers also liked (20)

PPT
Dot net guide for beginner
jayc8586
 
PPTX
C sharp
Ahmed Vic
 
PPT
C++ to java
Ajmal Ak
 
PPTX
Beginning Java for .NET developers
Andrei Rinea
 
PPT
Php i basic chapter 3
Muhamad Al Imran
 
PDF
A comparison between C# and Java
Ali MasudianPour
 
PPSX
Microsoft C# programming basics
Prognoz Technologies Pvt. Ltd.
 
PPTX
2.overview of c#
Raghu nath
 
PPT
Difference between C++ and Java
Ajmal Ak
 
PPT
Basics of c# by sabir
Sabir Ali
 
PPTX
C sharp
sanjay joshi
 
PPTX
C vs c++
ZTE Nepal
 
PPT
ASP.NET Session 1
Sisir Ghosh
 
PPTX
Java v/s .NET - Which is Better?
NIIT India
 
PPTX
Java script basic
Vithushan Vinayagamoorthy
 
PDF
Python overview
Hemant Kumar Tiwary
 
PPTX
C# basics
sagaroceanic11
 
PPTX
Java vs .net
Tech_MX
 
Dot net guide for beginner
jayc8586
 
C sharp
Ahmed Vic
 
C++ to java
Ajmal Ak
 
Beginning Java for .NET developers
Andrei Rinea
 
Php i basic chapter 3
Muhamad Al Imran
 
A comparison between C# and Java
Ali MasudianPour
 
Microsoft C# programming basics
Prognoz Technologies Pvt. Ltd.
 
2.overview of c#
Raghu nath
 
Difference between C++ and Java
Ajmal Ak
 
Basics of c# by sabir
Sabir Ali
 
C sharp
sanjay joshi
 
C vs c++
ZTE Nepal
 
ASP.NET Session 1
Sisir Ghosh
 
Java v/s .NET - Which is Better?
NIIT India
 
Java script basic
Vithushan Vinayagamoorthy
 
Python overview
Hemant Kumar Tiwary
 
C# basics
sagaroceanic11
 
Java vs .net
Tech_MX
 
Ad

Similar to Python basic (20)

PPTX
Python
MeHak Gulati
 
PPT
Help with Pyhon Programming Homework
Helpmeinhomework
 
ODP
An Intro to Python in 30 minutes
Sumit Raj
 
PPTX
Unit -1 CAP.pptx
malekaanjum1
 
PPT
python fundamental for beginner course .ppt
samuelmegerssa1
 
PPTX
Python programing
hamzagame
 
PPTX
Bikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa Thapa
 
PDF
python.pdf
BurugollaRavi1
 
PDF
pythonQuick.pdf
PhanMinhLinhAnxM0190
 
PDF
python 34đź’­.pdf
AkashdeepBhattacharj1
 
PDF
python notes.pdf
RohitSindhu10
 
PPTX
made it easy: python quick reference for beginners
SumanMadan4
 
PPTX
Introduction to Python Programming
VijaySharma802
 
PDF
Cs4hs2008 track a-programming
Rashi Agarwal
 
PPTX
lecture 2.pptx
Anonymous9etQKwW
 
DOCX
A Introduction Book of python For Beginners.docx
kumarrabinderkumar77
 
PDF
Introduction to Python Programming | InsideAIML
VijaySharma802
 
PDF
Notes1
hccit
 
PPT
Python
Kumar Gaurav
 
ODP
Introduction to Python - Training for Kids
Aimee Maree
 
Python
MeHak Gulati
 
Help with Pyhon Programming Homework
Helpmeinhomework
 
An Intro to Python in 30 minutes
Sumit Raj
 
Unit -1 CAP.pptx
malekaanjum1
 
python fundamental for beginner course .ppt
samuelmegerssa1
 
Python programing
hamzagame
 
Bikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa Thapa
 
python.pdf
BurugollaRavi1
 
pythonQuick.pdf
PhanMinhLinhAnxM0190
 
python 34đź’­.pdf
AkashdeepBhattacharj1
 
python notes.pdf
RohitSindhu10
 
made it easy: python quick reference for beginners
SumanMadan4
 
Introduction to Python Programming
VijaySharma802
 
Cs4hs2008 track a-programming
Rashi Agarwal
 
lecture 2.pptx
Anonymous9etQKwW
 
A Introduction Book of python For Beginners.docx
kumarrabinderkumar77
 
Introduction to Python Programming | InsideAIML
VijaySharma802
 
Notes1
hccit
 
Python
Kumar Gaurav
 
Introduction to Python - Training for Kids
Aimee Maree
 
Ad

Recently uploaded (20)

PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PDF
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
PDF
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
PPTX
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
community health nursing question paper 2.pdf
Prince kumar
 
PDF
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PDF
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PPSX
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
community health nursing question paper 2.pdf
Prince kumar
 
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 

Python basic

  • 1. Python -Mayur S. Mohite
  • 2. Many languages require you to compile (translate) your program into a form that the machine understands. compile execute source code byte code output Hello.java Hello.class Python is instead directly interpreted into machine instructions. interpret source code output Hello.py
  • 3. Storing Values Variable : A named piece of memory that can store a value. Usage : Compute an expression's result, store that result into a variable and use that variable later in the program. Assignment statement : Stores a value into a variable. Syntax : var_name = value Examples : x=5 A variable that has been given a value can be used in expressions. Eg. x + 4 is 9
  • 4. Printing Values Print : Produces text output on the console. Syntax : print "Message" print Expression Prints the given text message or expression value on the console, and moves the cursor down to the next line. Examples : print "Hello, world!" age = 45 print "You have", 65 - age, "years until retirement" Output : Hello, world! You have 20 years until retirement
  • 5. Input input : Reads a number from user input. raw_input : Reads a string of text from user input. Example : age = input("How old are you? ") print "Your age is", age Output: How old are you? 53 Your age is 53 name = raw_input("Howdy, pardner. What's yer name?") print name, "... what a silly name!" Output: Howdy, pardner. What's yer name? Paris Hilton Paris Hilton ... what a silly name!
  • 6. Loops for loop : Repeats a set of statements over a group of values. Example: for x in range(1, 6): print x, "squared is", x * x if statement : Executes a group of statements only if a certain condition is true. Otherwise, the statements are skipped. Example: if gpa > 2.0: print "Your application is accepted."
  • 7. if/else statement : Executes one block of statements if a certain condition is True, and a second block of statements if it is False. Example: if gpa > 2.0: print "Welcome to Mars University!" else: print "Your application is denied." while loop : Executes a group of statements as long as a condition is True. good for indefinite loops (repeat an unknown number of times) Example: number = 1 while number < 200: print number, number = number * 2
  • 8. String String : A sequence of text characters in a program. Strings start and end with quotation mark " or apostrophe ' characters. Examples: "hello" "This is a string" "This, too, is a string. It can be very long!" A string may not span across multiple lines or contain a " character. "This is not a legal String." "This is not a "legal" String either."
  • 9. File Handling Many programs handle data, which often comes from files. Reading the entire contents of a file: variableName = open("filename").read() Example: file_text = open("bankaccount.txt").read() Reading a file line-by-line: for line in open("filename").readlines(): statements Example: count = 0 for line in open("bankaccount.txt").readlines(): count = count + 1 print "The file contains", count, "lines."
  • 10. Classes And Objects A class acts like a container for all the methods and variable A class definition looks like this: class Point: pass Creating a new instance is called instantiation. To instantiate a Point object, we call a function named Point: blank = Point() We can add new data to an instance using dot notation: >>> blank.x = 3.0 >>> blank.y = 4.0