UP | HOME

Video 11: python if

Table of Contents

Introduction

Using if    if

if True:
    print 'yes'
if False:
    print 'yes'

else

if True:
    print 'yes'
else:
    print 'no'
if False:
    print 'yes'
else:
    print 'no'

A special one line if

'equal' if 1==0 else 'really not-equal'
print 'equal' if 1==0 else 'really not-equal'
answer = 'equal' if 1==0 else 'really not-equal'

Making a function    function

You will want to break you problem down into sections. One way to do that is to write functions.

def hello():
    print 'hello world function'

# Call it
hello()
def add_one(number):
    new_number = number + 1
    return new_number

# Calling our function.  Pass in the number 9
add_one(9)

IM-Python menu in emacs

Emacs knows some about your code. Now that you have functions, do the following with the menus:

IM-Python -> *rescan*

Now if you look under IM-Python, it should have your functions. If you go to:

Tools -> Source Code Parsers (Semantic)

Select it so that there is a check next to it and do another rescan. You will see other options under the IM-Python menu for variables and other parts of your code.

Separating groups of code    comments

One easy way to to put a line of comments. Try this emacs command:

C-u 50 #

You should see:

##################################################

C-u and a number before pressing a character will repeat that character that many times.

Doc strings

Final code

import sys

########################################
mylist = [1,10,'hi']

########################################
def hello_world():
    'Print hello world.  Pretty boring'
    print 'hello world'

def end_of_world():
    print 'good bye'

########################################
def main():
    '''
    This is a multiline description.

    Write lots and lots about the function here.
    '''
    print 'start of main'
    print 'sys.argv:', sys.argv
    for arg_number, argument in enumerate(sys.argv):
        if 'hell' in argument:
            print '----> skip hell'
            continue
        if '2' in argument:
            print '----> give up'
            break
        print 'an argument:', argument, 'is arg num:', arg_number
    print 'done with for'

######################################################################
if __name__ == '__main__':
    # Act like a program
    main()

History

The ipython history from the video:

if True:
     print 'yes'

if False:
     print 'yes'

if True:
     print 'yes'
else:
     print 'no'

if False:
    print 'yes'
else:
    print 'no'

type (False)
type (True)

1==1
1!=1
1>1
1>=1

# (1==1 && 2==2) # Nope!  This line is not python
(1==1 and 2==2)
(1==1 or 2==2)
(1!=1 or 2!=2)
'hell' in 'hello world'
4 in [1,2,6,7]
4 in [1,2,6,7,4]
'equal' if 1==0 else 'not really equal to'
print '1==0 is ', 'equal' if 1==0 else 'not really equal to', ' and more text'
print '1==1 is ', 'equal' if 1==1 else 'not really equal to', ' and more text'
answer = 'yes' if True else 'no'
answer
answer = 'yes' if 10 in (1,4,5,6) else 'no'
answer
print __name__
run try_args.py
import try_args
run try_args.py
reload(try_args)
reload(try_args)
try_args.main()
import sys
?sys.argv
sys.argv
run try_args.py
run try_args.py hello world
run try_args.py hello world 1 2 1000
help(sys)
reload(try_args)
?try_args.hello_world

Author: Kurt Schwehr

Date: <2011-10-04 Tue>

HTML generated by org-mode 7.4 in emacs 23