Python Tutorials

Overview

Variables can have a short names (like x and y) or a longer, more descriptive name (like age, firstname, phone_number).

Rules for Python variables:

  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variable names are case-sensitive (age, Age and AGE are three different variables)
  • A variable name cannot be any of the Python keywords

Allowed:

myvar = "Johnny"
my_var = "Johnny"
_my_var = "Johnny"
myVar = "Johnny"
MYVAR = "Johnny"
myvar2 = "Johnny"

Not Allowed:

2myvar = "Johnny" #cannot start with a number
my-var = "Johnny" #hyphens not allowed
my var = "Johnny" #spaces not allowed

Note

Variables ARE case sensitive

a = 8
A = "Johnny"
#A will NOT overwrite a

Camel, Pascal, and Snake Case

  • Camel case – each word, except the first, starts with a capital letter
  • Pascal case – each word starts with a capital letter
  • Snake case – each word is separated by an underscore character
myVariableName = "Johnny" #camel case
MyVariableName = "Johnny" #pascal case
my_variable_name = "Johnny" #snake case

Python Notes:

  • The most recent major version of Python is Python 3; however, Python 2 is still in use and quite popular, although not being updated with anything other than security updates
  • Python uses new lines to complete a command, as opposed to other programming languages which often use semicolons or parentheses
  • Python relies on indentation, using whitespace to define scope, such as the scope of loops, functions, and classes; other programming languages often use curly-brackets for this purpose
  • Python string methods return new values, and DO NOT change the original string

We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.