Introduction to Python


Contents

How to install Python on Linux
How to run a Python program
The basic of Python codes
Data expressions
Built-in functions in Python
Interactive input/output
Conditional statement for Python
Loop structure for Python
Creating your own functions
Python modules
Reading/writing files for Python
Error handling code
Object Oriented Programming (OOP)
Tkinter package for GNU programming


Conditional statement for Python

The conditional statement takes a collon after each command. Also the indents are part of the program to separate each section, so it must be ordered to be processed properly.
score=81
if score>=80:
   print "Passed!"
else:
   print "Failed..."
$ Passed!
The following is the equivalent program. Note that you can use "if" and "else" without colons:
print "Passed!" if score>=80 else "Failed..."
The "else if" command for python is "elif."
score1=65
if score1>=80:
   print "A"
elif score1>=70:
   print "B"
elif score1>=60:
   print "C"
else:
   print "F"
$ Keep making efforts.
The condition can have a range as follows:
if 60<score1<79:
   print "Keep making efforts.\n"
Boolean operators can be used for the conditions.
a=30
if (a>40) or (a<):
   print "The value is in the range.\n"
else:
   print "The value is out of the range.\n"
$ The value is out of the range.





Back to Electronics Page

Back to Hiro's Physics Main