2.1. Python Syntax - Basics #1#

In this section we’ll cover setting a variable with naming conventions and reserved words, the print function, and adding code comments. A good resource for additional information about the Python programming language in the atmospheric sciences is “Python Programming and Visualization for Scientists, 2nd Ed.” by Alex DeCaria.

Variable Assignment#

In Python you set a variable using the equal sign and the variable will assume a type based on the value assigned to variable name.

variable_name = <value>
# Assumes type based on value given
# Following is a string (character) type
name = 'Kevin'

# Following is an integer type
temp_celsius = 20

# Following is a float type
temp_fahrenheit = 72.5

Note

Be careful when setting variable names. All names should begin with a letter and contain no spaces. After the first letter, any combination of letters and numbers can be used. To put a “space” in a name use an underscore (_). For example, the following is an okay variable name first_name. It is common practice to use only lower case letters in common variable names.

Reserved Words#

There are a number of words that you should not use to define a variable, becuase that words serves a very specific functionality within the Python language. The words to avoid are:

reserved

words

don’t

use

False

class

from

or

None

continue

global

pass

True

def

if

raise

and

del

import

return

as

elif

in

try

assert

else

is

while

async

except

lambda

with

await

finally

nonlocal

yield

break

for

not

To find out more about any element of a Python function, use the help() function.

help('keywords')
Here is a list of the Python keywords.  Enter any keyword to get more help.

False               class               from                or
None                continue            global              pass
True                def                 if                  raise
and                 del                 import              return
as                  elif                in                  try
assert              else                is                  while
async               except              lambda              with
await               finally             nonlocal            yield
break               for                 not                 

Code Comments#

It is imperative that you comment your code to ensure you remember what pieces of your code are doing. This will help when you come back to old code after a period of time or share your code with someone else. Comments can be made in two primary ways, through stand alone comments on one line or inline, either way you want to use the special character #, which is officially known as an octothorp or better known to you as a hashtag.

Any line that starts with a # will not be executed at runtime. Any line that has a # in it, nothing will be executed after that symbol.

# This is a comment and won't be executed
# The following line is executable code
print('Hello') # this is an inline comment

Output

Hello
# Setting a temperature value
tmpc = 28
print(f'The current temperature is {tmpc}C') # Printing out the current temperature
The current temperature is 28C