Intro to CS with Python— Python Basic Syntax

Python is a gerneral-purpose interpreted, interactive, object-oriented, and high-level programming language. This series is an introductory Computer Science (CS) tutorial designed for anyone who is interested in Programming, Problem Solving, Algorithms, Data Structures and Software Development.

There are many reasons why Python is one of the most popular programming languages that is being used today. This language can be utilized for a wide range of applications like scripting, developing and testing. Because of its elegance and simplicity, top technology organizations like Google, Dropbox, Youtube, Mozilla, HP, IBM, Cisco and many more have implemented Python. I won’t get down to the nitty-gritty details of the reasons why it’s one of the best languages to learn today but you will quickly see how powerful Python is.

Python Identifiers

A python identifier is a name used to identify a variable, function, class, module or any objects, basically anything you get to name yourself.

In Python, identifier

  • Starts with an uppercase letter ‘A’ to ‘Z’ or an lowercase letter ‘a’ to ‘z’ or an underscore(‘_’) followed by more letters or digits (‘0’ to ‘9’)
  • Cannot have any special characters such as !, @, #, $, %, etc.
  • is case sensitive. (A != a)

Naming Conventions in Python

  • Make sure to check out the Style Guide for Python Code.
  • It is really important to follow the naming conventions for the language you are using.

Reserved words (Keywords) in Python

The Following list shows the Python keywords. You cannot use a keyword as variable name, function name or any other identifier.

Keywords in Python

Lines and Indentation

Unlike programming languages like C or Java, Python does not use braces {} to mark blocks of code for functions or classes or loops or conditional statements. Blocks of code are indicated by line indentation. Compound statements like conditional statements (if-else), function definition (def) or loops (for, while loops) and class require a header line that begins the statement with the keyword and ends with a colon(:). Header lines are followed by one or more lines with make up the block.

if <<expression>>:
elif <<expression>>:
def <<function_name>>():
class <<class_name>>:

Multi-Line Statements

Each statement in Python typically ends with a new line. But, you can still use the line continuation character ‘\’ to indicate that the line should continue.

For example,

total_age = person1 + \
person2 + \
person3 + \
person4 + \

The statements contained within the [], {}, () brackets do not need to use the line continuation character.

For example,

friends = ["Leonardo", 

Comments in Python

# Comment should start with a hash sign like this!
print("Vancouver!") # You can also add a comment on the same line!