SlideShare a Scribd company logo
Python Programming
Variables, Expressions and Statements
By
Vikram Neerugatti
Assistant Professor
SVCET, Chittoor, Andhra Pradesh.
Content
• Variables
• Variable names
• Assignment statements
• Expressions and statements
• Script mode
• Order of operations
• String operations
• Comments
• Input from user
31-12-2019 Vikram Neerugatti, Assistant Professor, SVCET, Chittor. 2
Variables
• The value is a number or string.
• Variable is a name that refers to a value.
• Assignment statement will create new variable to the values.
• >>>> a=5
• >>>b= ‘tttt’
• >>>c=5.6
• Variable can be numbers or strings with numbers.
• It should start with the string not number
• No special symbols but _(underscore) can be used.
31-12-2019 Vikram Neerugatti, Assistant Professor, SVCET, Chittor. 3
Variables and names
• >>> 76trombones = 'big parade’
• SyntaxError: invalid syntax
• >>> more@ = 1000000
• SyntaxError: invalid syntax
• >>> class = 'Advanced Theoretical Zymurgy’
• SyntaxError: invalid syntax
• Keywords should not b the variable name
• Class, if, etc.
31-12-2019 Vikram Neerugatti, Assistant Professor, SVCET, Chittor. 4
Statements
• A statement is a unit of code that the Python interpreter can execute.
We have seen two kinds of statements: print and assignment.
• Interactive mode –one statement-result
• Script mode-two or more statements-result-for all statements at once
• Some statements will not produce ourput.
• Ex. Assignment statement
31-12-2019 Vikram Neerugatti, Assistant Professor, SVCET, Chittor. 5
Assignment statements
• With this value will be assigned to a variable.
• A=5
• = is a assignment operator
• It assign any type of value to the variable
• A=4
• B=‘2’
• C=,nani’
• D=(55,5,5)
31-12-2019 Vikram Neerugatti, Assistant Professor, SVCET, Chittor. 6
Expressions
• An expression is a combination of values, variables, and operators. A value all by itself is considered an expression, and so is a
variable, so the following are all legal expressions (assuming that the variable x has been assigned a value):
• 17
• x
• x + 17
• If you type an expression in interactive mode, the interpreter evaluates it and displays the result:
• >>> 1 + 1
• 2
• But in a script, an expression all by itself doesn't do anything! This is a common source of confusion for beginners.
• Exercise 1 Type the following statements in the Python interpreter to see what they do:
• 5
• x = 5
• x + 1
31-12-2019 Vikram Neerugatti, Assistant Professor, SVCET, Chittor. 7
Operators and operands
• Operators are special symbols that represent computations like addition and multiplication. The values the
operator is applied to are called operands.
• The operators +, -, *, / and ** perform addition, subtraction, multiplication, division and exponentiation, as
in the following examples:
• 20+32 hour-1 hour*60+minute minute/60 5**2 (5+9)*(15-7)
• The division operator might not do what you expect:
• >>> minute = 59
• >>> minute/60
• 0
• The value of minute is 59, and in conventional arithmetic 59 divided by 60 is 0.98333, not 0. The reason for
the discrepancy is that Python is performing floor division2.
• When both of the operands are integers, the result is also an integer; floor division chops off the fraction
part, so in this example it rounds down to zero.
• If either of the operands is a floating-point number, Python performs floating-point division, and the result is
a float:
• >>> minute/60.0
• 0.98333333333333328
31-12-2019 Vikram Neerugatti, Assistant Professor, SVCET, Chittor. 8
Order of operations
• When more than one operator appears in an expression, the order of evaluation depends on the rules of
precedence.
• For mathematical operators, Python follows mathematical convention. The acronym PEMDAS is a useful way
to remember the rules:
• Parentheses have the highest precedence and can be used to force an expression to evaluate in the order
you want. Since expressions in parentheses are evaluated first, 2 * (3-1) is 4, and (1+1)**(5-2) is 8. You can
also use parentheses to make an expression easier to read, as in (minute * 100) / 60, even if it doesn't
change the result.
• Exponentiation has the next highest precedence, so 2**1+1 is 3, not 4, and 3*1**3 is 3, not 27.
• Multiplication and Division have the same precedence, which is higher than Addition and Subtraction, which
also have the same precedence. So 2*3-1 is 5, not 4, and 6+4/2 is 8, not 5.
• Operators with the same precedence are evaluated from left to right. So in the expression 5-3-1 is 1, not 3
because the 5-3 happens first and then 1 is subtracted from 2.
• When in doubt always put parentheses in your expressions to make sure the computations are performed in
the order you intend.
31-12-2019 Vikram Neerugatti, Assistant Professor, SVCET, Chittor. 9
String Operations
• The + operator works with strings, but it is not addition in the
mathematical sense. Instead it performs concatenation, which means
joining the strings by linking them end-to-end.
• For example:
• >>> first = 10
• >>> second = 15
• >>> print first+second
• 25
• >>> first = '100'
• >>> second = '150'
• >>> print first + second
• 100150
31-12-2019 Vikram Neerugatti, Assistant Professor, SVCET, Chittor. 10
Comments
• As programs get bigger and more complicated, they get more difficult to read. Formal languages
are dense, and it is often difficult to look at a piece of code and figure out what it is doing, or why.
• For this reason, it is a good idea to add notes to your programs to explain in natural language
what the program is doing. These notes are called comments, and they start with the # symbol:
• # compute the percentage of the hour that has elapsed
• percentage = (minute * 100) / 60
• In this case, the comment appears on a line by itself. You can also put comments at the end of a
line:
• percentage = (minute * 100) / 60 # percentage of an hour
• Everything from the # to the end of the line is ignored---it has no effect on the program.
• Comments are most useful when they document non-obvious features of the code. It is
reasonable to assume that the reader can figure out what the code does; it is much more useful
to explain why.
• This comment is redundant with the code and useless:
31-12-2019 Vikram Neerugatti, Assistant Professor, SVCET, Chittor. 11
Input from user
• Sometimes we would like to take the value for a variable from the user via their keyboard.
• Python provides a built-in function called raw_input that gets input from the keyboard3. When this function
is called, the program stops and waits for the user to type something. When the user presses Return or
Enter, the program resumes and raw_input returns what the user typed as a string.
• >>> input = raw_input()
• Some silly stuff
• >>> print input
• Some silly stuff
• Before getting input from the user, it is a good idea to print a prompt telling the user what to input. You can
pass a string to raw_input to be displayed to the user before pausing for input:
• >>> name = raw_input('What is your name?n')
• What is your name?
• Chuck
• >>> print name
• Chuck
• The sequence n at the end of the prompt represents a newline, which is a special character that causes a
line break. That's why the user's input appears below the prompt.
31-12-2019 Vikram Neerugatti, Assistant Professor, SVCET, Chittor. 12
Summary
• Variables
• Variable names
• Assignment statements
• Expressions and statements
• Script mode
• Order of operations
• String operations
• Comments
• Input from user
31-12-2019 Vikram Neerugatti, Assistant Professor, SVCET, Chittor. 13
Any questions?
• Vikram.nandu143@gmail.com
• Www.vikramneerugatti.com
31-12-2019 Vikram Neerugatti, Assistant Professor, SVCET, Chittor. 14
Thank you
31-12-2019 Vikram Neerugatti, Assistant Professor, SVCET, Chittor. 15

More Related Content

What's hot (19)

PPTX
Intro to Data Structure & Algorithms
Akhil Kaushik
 
PDF
Operators in java
Ravi_Kant_Sahu
 
PPT
Ap Power Point Chpt2
dplunkett
 
PPT
Python programming
saroja20
 
PPTX
Python Data-Types
Akhil Kaushik
 
PDF
+2 Computer Science - Volume II Notes
Andrew Raj
 
PPTX
CPP07 - Scope
Michael Heron
 
PPTX
Python programming
saroja20
 
PPT
Ap Power Point Chpt8
dplunkett
 
PDF
[YIDLUG] Programming Languages Differences, The Underlying Implementation 1 of 2
Yo Halb
 
PDF
L2 datatypes and variables
Ravi_Kant_Sahu
 
PDF
Lesson 02 python keywords and identifiers
Nilimesh Halder
 
PDF
Data handling CBSE PYTHON CLASS 11
chinthala Vijaya Kumar
 
PPTX
Python second ppt
RaginiJain21
 
PPTX
11 Unit 1 Chapter 03 Data Handling
Praveen M Jigajinni
 
PPTX
Java 8 Functional Programming - I
Ugur Yeter
 
PDF
Lecture 4 variables data types and operators
alvin567
 
PDF
C programming | Class 8 | III Term
Andrew Raj
 
DOCX
Notes on c++
Selvam Edwin
 
Intro to Data Structure & Algorithms
Akhil Kaushik
 
Operators in java
Ravi_Kant_Sahu
 
Ap Power Point Chpt2
dplunkett
 
Python programming
saroja20
 
Python Data-Types
Akhil Kaushik
 
+2 Computer Science - Volume II Notes
Andrew Raj
 
CPP07 - Scope
Michael Heron
 
Python programming
saroja20
 
Ap Power Point Chpt8
dplunkett
 
[YIDLUG] Programming Languages Differences, The Underlying Implementation 1 of 2
Yo Halb
 
L2 datatypes and variables
Ravi_Kant_Sahu
 
Lesson 02 python keywords and identifiers
Nilimesh Halder
 
Data handling CBSE PYTHON CLASS 11
chinthala Vijaya Kumar
 
Python second ppt
RaginiJain21
 
11 Unit 1 Chapter 03 Data Handling
Praveen M Jigajinni
 
Java 8 Functional Programming - I
Ugur Yeter
 
Lecture 4 variables data types and operators
alvin567
 
C programming | Class 8 | III Term
Andrew Raj
 
Notes on c++
Selvam Edwin
 

Similar to Python unit 1 part-2 (20)

PPTX
Python Lecture 2
Inzamam Baig
 
PDF
Variables in Python & Data Types and Their Values
Raza Ul Mustafa
 
PDF
03-Variables, Expressions and Statements (1).pdf
MirHazarKhan1
 
PPTX
Introduction to Python Values, Variables Data Types Chapter 2
Raza Ul Mustafa
 
PPTX
Pythonlearn-02-Expressions123AdvanceLevel.pptx
AninditaSarkarNaha
 
PPT
17575602.ppt
TejaValmiki
 
PDF
python2oxhvoudhuSGFsughusgdogusuosFU.pdf
rohithzach
 
PPTX
Lec2_cont.pptx galgotias University questions
YashJain47002
 
PPT
PythonCourse_02_Expressions.ppt Python introduction turorial for beginner.
sakchaisengsui
 
PPTX
PPT_1_9102501a-a7a1-493e-818f-cf699918bbf6.pptx
myatminsoe180
 
PPTX
python notes for MASTER OF COMPUTER APPLIICATION_ppt.pptx
yuvarajkumar334
 
PPTX
MODULE. .pptx
Alpha337901
 
PDF
PPE-Module-1.2 PPE-Module-1.2 PPE-Module-1.2.pdf
ArjayBalberan1
 
PPTX
Engineering CS 5th Sem Python Module-1.pptx
hardii0991
 
PPTX
Lecture 1 .
SwatiHans10
 
PPTX
unit1 python.pptx
TKSanthoshRao
 
PPTX
parts_of_python_programming_language.pptx
Koteswari Kasireddy
 
PPTX
Review old Pygame made using python programming.pptx
ithepacer
 
PPTX
Python Operators.pptx
M Vishnuvardhan Reddy
 
PPTX
Python-Certification-Training-Day-1-2.pptx
muzammildev46gmailco
 
Python Lecture 2
Inzamam Baig
 
Variables in Python & Data Types and Their Values
Raza Ul Mustafa
 
03-Variables, Expressions and Statements (1).pdf
MirHazarKhan1
 
Introduction to Python Values, Variables Data Types Chapter 2
Raza Ul Mustafa
 
Pythonlearn-02-Expressions123AdvanceLevel.pptx
AninditaSarkarNaha
 
17575602.ppt
TejaValmiki
 
python2oxhvoudhuSGFsughusgdogusuosFU.pdf
rohithzach
 
Lec2_cont.pptx galgotias University questions
YashJain47002
 
PythonCourse_02_Expressions.ppt Python introduction turorial for beginner.
sakchaisengsui
 
PPT_1_9102501a-a7a1-493e-818f-cf699918bbf6.pptx
myatminsoe180
 
python notes for MASTER OF COMPUTER APPLIICATION_ppt.pptx
yuvarajkumar334
 
MODULE. .pptx
Alpha337901
 
PPE-Module-1.2 PPE-Module-1.2 PPE-Module-1.2.pdf
ArjayBalberan1
 
Engineering CS 5th Sem Python Module-1.pptx
hardii0991
 
Lecture 1 .
SwatiHans10
 
unit1 python.pptx
TKSanthoshRao
 
parts_of_python_programming_language.pptx
Koteswari Kasireddy
 
Review old Pygame made using python programming.pptx
ithepacer
 
Python Operators.pptx
M Vishnuvardhan Reddy
 
Python-Certification-Training-Day-1-2.pptx
muzammildev46gmailco
 
Ad

More from Vikram Nandini (20)

PDF
IoT: From Copper strip to Gold Bar
Vikram Nandini
 
PDF
Design Patterns
Vikram Nandini
 
PDF
Linux File Trees and Commands
Vikram Nandini
 
PDF
Introduction to Linux & Basic Commands
Vikram Nandini
 
PDF
INTRODUCTION to OOAD
Vikram Nandini
 
PDF
Ethics
Vikram Nandini
 
PDF
Manufacturing - II Part
Vikram Nandini
 
PDF
Manufacturing
Vikram Nandini
 
PDF
Business Models
Vikram Nandini
 
PDF
Prototyping Online Components
Vikram Nandini
 
PDF
Artificial Neural Networks
Vikram Nandini
 
PDF
IoT-Prototyping
Vikram Nandini
 
PDF
Design Principles for Connected Devices
Vikram Nandini
 
PDF
Introduction to IoT
Vikram Nandini
 
PDF
Embedded decices
Vikram Nandini
 
PDF
Communication in the IoT
Vikram Nandini
 
PDF
Introduction to Cyber Security
Vikram Nandini
 
PDF
cloud computing UNIT-2.pdf
Vikram Nandini
 
PDF
Introduction to Web Technologies
Vikram Nandini
 
PDF
Cascading Style Sheets
Vikram Nandini
 
IoT: From Copper strip to Gold Bar
Vikram Nandini
 
Design Patterns
Vikram Nandini
 
Linux File Trees and Commands
Vikram Nandini
 
Introduction to Linux & Basic Commands
Vikram Nandini
 
INTRODUCTION to OOAD
Vikram Nandini
 
Manufacturing - II Part
Vikram Nandini
 
Manufacturing
Vikram Nandini
 
Business Models
Vikram Nandini
 
Prototyping Online Components
Vikram Nandini
 
Artificial Neural Networks
Vikram Nandini
 
IoT-Prototyping
Vikram Nandini
 
Design Principles for Connected Devices
Vikram Nandini
 
Introduction to IoT
Vikram Nandini
 
Embedded decices
Vikram Nandini
 
Communication in the IoT
Vikram Nandini
 
Introduction to Cyber Security
Vikram Nandini
 
cloud computing UNIT-2.pdf
Vikram Nandini
 
Introduction to Web Technologies
Vikram Nandini
 
Cascading Style Sheets
Vikram Nandini
 
Ad

Recently uploaded (20)

PPTX
Gall bladder, Small intestine and Large intestine.pptx
rekhapositivity
 
PPTX
Nutrition Month 2025 TARP.pptx presentation
FairyLouHernandezMej
 
PPSX
Health Planning in india - Unit 03 - CHN 2 - GNM 3RD YEAR.ppsx
Priyanshu Anand
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PPTX
classroom based quiz bee.pptx...................
ferdinandsanbuenaven
 
PPTX
CONVULSIVE DISORDERS: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
PPTX
How to Define Translation to Custom Module And Add a new language in Odoo 18
Celine George
 
PDF
IMP NAAC-Reforms-Stakeholder-Consultation-Presentation-on-Draft-Metrics-Unive...
BHARTIWADEKAR
 
PPTX
PPT on the Development of Education in the Victorian England
Beena E S
 
PPTX
How to Manage Access Rights & User Types in Odoo 18
Celine George
 
PPTX
LEGAL ASPECTS OF PSYCHIATRUC NURSING.pptx
PoojaSen20
 
PDF
Federal dollars withheld by district, charter, grant recipient
Mebane Rash
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PPTX
nutriquiz grade 4.pptx...............................................
ferdinandsanbuenaven
 
PPTX
Pyhton with Mysql to perform CRUD operations.pptx
Ramakrishna Reddy Bijjam
 
PPTX
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
PDF
BÀI TẬP BỔ TRỢ THEO LESSON TIẾNG ANH - I-LEARN SMART WORLD 7 - CẢ NĂM - CÓ ĐÁ...
Nguyen Thanh Tu Collection
 
PPTX
Capitol Doctoral Presentation -July 2025.pptx
CapitolTechU
 
PPTX
Maternal and Child Tracking system & RCH portal
Ms Usha Vadhel
 
PPT
digestive system for Pharm d I year HAP
rekhapositivity
 
Gall bladder, Small intestine and Large intestine.pptx
rekhapositivity
 
Nutrition Month 2025 TARP.pptx presentation
FairyLouHernandezMej
 
Health Planning in india - Unit 03 - CHN 2 - GNM 3RD YEAR.ppsx
Priyanshu Anand
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
classroom based quiz bee.pptx...................
ferdinandsanbuenaven
 
CONVULSIVE DISORDERS: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
How to Define Translation to Custom Module And Add a new language in Odoo 18
Celine George
 
IMP NAAC-Reforms-Stakeholder-Consultation-Presentation-on-Draft-Metrics-Unive...
BHARTIWADEKAR
 
PPT on the Development of Education in the Victorian England
Beena E S
 
How to Manage Access Rights & User Types in Odoo 18
Celine George
 
LEGAL ASPECTS OF PSYCHIATRUC NURSING.pptx
PoojaSen20
 
Federal dollars withheld by district, charter, grant recipient
Mebane Rash
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
nutriquiz grade 4.pptx...............................................
ferdinandsanbuenaven
 
Pyhton with Mysql to perform CRUD operations.pptx
Ramakrishna Reddy Bijjam
 
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
BÀI TẬP BỔ TRỢ THEO LESSON TIẾNG ANH - I-LEARN SMART WORLD 7 - CẢ NĂM - CÓ ĐÁ...
Nguyen Thanh Tu Collection
 
Capitol Doctoral Presentation -July 2025.pptx
CapitolTechU
 
Maternal and Child Tracking system & RCH portal
Ms Usha Vadhel
 
digestive system for Pharm d I year HAP
rekhapositivity
 

Python unit 1 part-2

  • 1. Python Programming Variables, Expressions and Statements By Vikram Neerugatti Assistant Professor SVCET, Chittoor, Andhra Pradesh.
  • 2. Content • Variables • Variable names • Assignment statements • Expressions and statements • Script mode • Order of operations • String operations • Comments • Input from user 31-12-2019 Vikram Neerugatti, Assistant Professor, SVCET, Chittor. 2
  • 3. Variables • The value is a number or string. • Variable is a name that refers to a value. • Assignment statement will create new variable to the values. • >>>> a=5 • >>>b= ‘tttt’ • >>>c=5.6 • Variable can be numbers or strings with numbers. • It should start with the string not number • No special symbols but _(underscore) can be used. 31-12-2019 Vikram Neerugatti, Assistant Professor, SVCET, Chittor. 3
  • 4. Variables and names • >>> 76trombones = 'big parade’ • SyntaxError: invalid syntax • >>> more@ = 1000000 • SyntaxError: invalid syntax • >>> class = 'Advanced Theoretical Zymurgy’ • SyntaxError: invalid syntax • Keywords should not b the variable name • Class, if, etc. 31-12-2019 Vikram Neerugatti, Assistant Professor, SVCET, Chittor. 4
  • 5. Statements • A statement is a unit of code that the Python interpreter can execute. We have seen two kinds of statements: print and assignment. • Interactive mode –one statement-result • Script mode-two or more statements-result-for all statements at once • Some statements will not produce ourput. • Ex. Assignment statement 31-12-2019 Vikram Neerugatti, Assistant Professor, SVCET, Chittor. 5
  • 6. Assignment statements • With this value will be assigned to a variable. • A=5 • = is a assignment operator • It assign any type of value to the variable • A=4 • B=‘2’ • C=,nani’ • D=(55,5,5) 31-12-2019 Vikram Neerugatti, Assistant Professor, SVCET, Chittor. 6
  • 7. Expressions • An expression is a combination of values, variables, and operators. A value all by itself is considered an expression, and so is a variable, so the following are all legal expressions (assuming that the variable x has been assigned a value): • 17 • x • x + 17 • If you type an expression in interactive mode, the interpreter evaluates it and displays the result: • >>> 1 + 1 • 2 • But in a script, an expression all by itself doesn't do anything! This is a common source of confusion for beginners. • Exercise 1 Type the following statements in the Python interpreter to see what they do: • 5 • x = 5 • x + 1 31-12-2019 Vikram Neerugatti, Assistant Professor, SVCET, Chittor. 7
  • 8. Operators and operands • Operators are special symbols that represent computations like addition and multiplication. The values the operator is applied to are called operands. • The operators +, -, *, / and ** perform addition, subtraction, multiplication, division and exponentiation, as in the following examples: • 20+32 hour-1 hour*60+minute minute/60 5**2 (5+9)*(15-7) • The division operator might not do what you expect: • >>> minute = 59 • >>> minute/60 • 0 • The value of minute is 59, and in conventional arithmetic 59 divided by 60 is 0.98333, not 0. The reason for the discrepancy is that Python is performing floor division2. • When both of the operands are integers, the result is also an integer; floor division chops off the fraction part, so in this example it rounds down to zero. • If either of the operands is a floating-point number, Python performs floating-point division, and the result is a float: • >>> minute/60.0 • 0.98333333333333328 31-12-2019 Vikram Neerugatti, Assistant Professor, SVCET, Chittor. 8
  • 9. Order of operations • When more than one operator appears in an expression, the order of evaluation depends on the rules of precedence. • For mathematical operators, Python follows mathematical convention. The acronym PEMDAS is a useful way to remember the rules: • Parentheses have the highest precedence and can be used to force an expression to evaluate in the order you want. Since expressions in parentheses are evaluated first, 2 * (3-1) is 4, and (1+1)**(5-2) is 8. You can also use parentheses to make an expression easier to read, as in (minute * 100) / 60, even if it doesn't change the result. • Exponentiation has the next highest precedence, so 2**1+1 is 3, not 4, and 3*1**3 is 3, not 27. • Multiplication and Division have the same precedence, which is higher than Addition and Subtraction, which also have the same precedence. So 2*3-1 is 5, not 4, and 6+4/2 is 8, not 5. • Operators with the same precedence are evaluated from left to right. So in the expression 5-3-1 is 1, not 3 because the 5-3 happens first and then 1 is subtracted from 2. • When in doubt always put parentheses in your expressions to make sure the computations are performed in the order you intend. 31-12-2019 Vikram Neerugatti, Assistant Professor, SVCET, Chittor. 9
  • 10. String Operations • The + operator works with strings, but it is not addition in the mathematical sense. Instead it performs concatenation, which means joining the strings by linking them end-to-end. • For example: • >>> first = 10 • >>> second = 15 • >>> print first+second • 25 • >>> first = '100' • >>> second = '150' • >>> print first + second • 100150 31-12-2019 Vikram Neerugatti, Assistant Professor, SVCET, Chittor. 10
  • 11. Comments • As programs get bigger and more complicated, they get more difficult to read. Formal languages are dense, and it is often difficult to look at a piece of code and figure out what it is doing, or why. • For this reason, it is a good idea to add notes to your programs to explain in natural language what the program is doing. These notes are called comments, and they start with the # symbol: • # compute the percentage of the hour that has elapsed • percentage = (minute * 100) / 60 • In this case, the comment appears on a line by itself. You can also put comments at the end of a line: • percentage = (minute * 100) / 60 # percentage of an hour • Everything from the # to the end of the line is ignored---it has no effect on the program. • Comments are most useful when they document non-obvious features of the code. It is reasonable to assume that the reader can figure out what the code does; it is much more useful to explain why. • This comment is redundant with the code and useless: 31-12-2019 Vikram Neerugatti, Assistant Professor, SVCET, Chittor. 11
  • 12. Input from user • Sometimes we would like to take the value for a variable from the user via their keyboard. • Python provides a built-in function called raw_input that gets input from the keyboard3. When this function is called, the program stops and waits for the user to type something. When the user presses Return or Enter, the program resumes and raw_input returns what the user typed as a string. • >>> input = raw_input() • Some silly stuff • >>> print input • Some silly stuff • Before getting input from the user, it is a good idea to print a prompt telling the user what to input. You can pass a string to raw_input to be displayed to the user before pausing for input: • >>> name = raw_input('What is your name?n') • What is your name? • Chuck • >>> print name • Chuck • The sequence n at the end of the prompt represents a newline, which is a special character that causes a line break. That's why the user's input appears below the prompt. 31-12-2019 Vikram Neerugatti, Assistant Professor, SVCET, Chittor. 12
  • 13. Summary • Variables • Variable names • Assignment statements • Expressions and statements • Script mode • Order of operations • String operations • Comments • Input from user 31-12-2019 Vikram Neerugatti, Assistant Professor, SVCET, Chittor. 13
  • 14. Any questions? • [email protected] Www.vikramneerugatti.com 31-12-2019 Vikram Neerugatti, Assistant Professor, SVCET, Chittor. 14
  • 15. Thank you 31-12-2019 Vikram Neerugatti, Assistant Professor, SVCET, Chittor. 15