Back

Python Chrestomathics

Ahead


Part 2 — Basic anatomy

An anatomy — Needs to be undertaken — Of our first program

Do not be afraid — Do not hide what makes you, you — You are welcomed here

© from noun project anatomy

Section 2.1: Here we go

Communication — The essence of love and work — And now in Python

tin can talking hearts

All you need is love

""" Purpose: demonstrate user communication

  Author: Jim Cohoon

  Email id: jpc

"""

# Send message to user

print( "All you need is love" )

print( "All you need is love" )

The preceding lines are documentation. As mentioned above, they are there because of their importance when writing code.

""" Purpose: demonstrate user communication

  Author: Jim Cohoon

  Email id: jpc

"""

# Send message to user

is an explanatory comment. These comments often occur at the start of a new section of code to explain what is happening next, or within a section of code to provide detail.

print( "All you need is love" )

print( "All you need is love" )

Python first checks that the statement starts in column 1. It then causes the built-in print() function to be invoked. The function then displays its string argument "All you need is love".

recommendation icon

White space

The following statements are equivalent to Python for getting our love message displayed. The first one is our preference.

print( "All you need is love" )

print("All you need is love")

print( "All you need is love")

print("All you need is love" )

print(     "All you need is love"     )

  • The lack of spaces in the second statement requires more effort on the part of the reader to understand what is happening.
  • The third and fourth statements use different spacing strategies for opening and closing parentheses. Code, like sentences in an essay, should normally follow parallel structure. Lack of parallelism can irritate a reader.
  • The spacing in the last statement seems to be excessive in general.


Section 2.2: Onward

""" Purpose: demonstrate user communication

"""

# Display haiku

print( "Camouflage not needed" )

print()

print( "Do not be afraid" )

print( "Do not hide what makes you, you" )

print( "You are welcomed here" )

Camouflage not needed

Do not be afraid

Do not hide what makes you, you

You are welcomed here


Section 2.3: Just a little more

""" Purpose: display strings, integers, and decimal values

"""

print( 21, "Celsius is", 69.8, "Fahrenheit" )

21 Celsius is 69.8 Fahrenheit

print( "a,", "b, ", 1, 2, 12 )

print( "a a", " b")

a, b, 1 2 12

a a b

Print( "a" 1 "b" )

Print( a, b

Print( 'a", 'b" )

Print( "a", 'b' )



Section 2.4: What’s next


 

cover sleeve for sing all you need is love

The Beatles All You Need Is Love, © Capitol Records

 


Back

Ahead

 


© Jim Cohoon. For now, all rights reserved.