Python Syntax

Syntax are a set of rules that determine how you write your code. Python uses whitespace to determine where a block of code starts and ends; other languages use keywords or braces such as { }.

To demonstrate this, here is an example of a function. Don’t worry about what a function is right now, there is a section for that.

def foo(x):
    print(x)
    print(x*2)

 

If you were to write the code this way (see below), you would get a trace back error which is to notify you of an error in the code.

def foo(x): print(x) print(x*2)

 

Reserved Words

There are keywords in Python that cannot be used as an identifier. An identifier is the name that is given to variables, functions, methods, etc. These reserved keywords are:

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

Assignment Statement

An assignment statement is used to create a Python object. Nearly everything in Python is a Python object that has methods and/or attributes. When working with research or data science, most commonly these will be considered a variable or constant. It is important to know that these are Python objects because when we get working with data frames, a column within a data frame is sometimes referred to as a variable. In this case though, the term variable has a slightly different meaning. The general assignment statement is below.

your_Python_object = stuff

 

Variables and Constants

As one might expect, a variable is something that varies and a constant is something that does not. In Python, both of these can be assigned a name using the equal sign (=).

input [1] x = "Hello World"
input [2] x
output [3] "Hello World"

 

In this example, “x” is the name of the constant and it is assigned the phrase (which is a string type) “Hello World”. Any time the programmer (that’s you!) enters “x” Python will print “Hello World”.

Variables and constants can be reassigned values. Do note that once a variable or constant is reassigned, it no longer holds the previous values assigned to it.

 

Python Naming Rules

  1. Must start with a letter or underscore “_”
  2. Must consist of letters and numbers and underscores
  3. Case sensitive
    • I.e. “MyVar”, “myVar”, “myvar”, and “mYvAr” are all different variable names.

It is considered best practice to name variables and constants with a name that reminds the programmer (or somebody reviewing your code) what the variable or constant is.

asdfgtr = "Books"
touy = 5
print("Item name: ", asdfgtr, ", Quantity sold = ", touy)

 

Those variable names are hard to remember, and give the programmer no idea of what type of values they store. Better names would be something like this.

item = "Books"
quant_sold = 5
print("Item name: ", item, ", Quantity sold = ", quant_sold)

 

Data Types

There are a few built-in data types that you will encounter using Python for research and data science applications. These are:

  • int: Integers (whole numbers)
  • float: Floating point numbers (numbers with decimals)
  • str: String, which is a character type (i.e. “Hello”, “World”, and “Be brilliant!”). Anything between the “” is considered a string
  • boolean: True or False values. Commonly used in conditional expressions

You will be using the Pandas library with Python for research and data science applications and the Pandas library introduces more data types. These are:

  • object: which is equivalent to the str built-in data type
  • int64: which is equivalent to the int built-in data type
  • float64: which is equivalent to the float built-in data type
  • datetime64: a value that contains time date (seconds, minutes, hours, days, month, year, etc.)
  • timedelta: the difference between two datetime64 values