Python

Python Intermediate Fundamentals

Intermediate

This page covers the intermediate fundamentals of Python programming.

For Python basic fundamentals here.

It continues the previous basic tutorials in more details. Intermediate fundamentals encompass:

  • local and global variables
  • assigning multiple variables
  • naming variables
  • importing multiple modules and functions

Local and global variables

In terms of scope, there are two types of variables. These are local variables and global variables.

Local variables can only operate inside a function and global variables can operate anywhere in the code. The best example is to look at a common variable “a”.

a = 7
print(a)

# Output 7

The above example assigns the number 7 to a variable “a”. The variable is outside of any function and is therefore a global variable. If there is a function within the code, the variable will still work both inside and outside the function.

a = 7

def some_func():
    print(a)
    
some_func()

# Output 7

The following example is a function with a local variable (the variable is inside the function).

def some_func():
    a = 7
    print(a)
    
some_func()

# Output 7

When the variable is assigned inside the function, then using the variable outside the function will not work.

def some_func():
    b = 7
    
print(b)

# Output NameError: name 'b' is not defined

Creating a variable inside a function and trying to use it outside the function (in the global scope), Python will always produce a NameError.

Luckily, in Python local variables inside a function can still work outside the function. This can happen with the keyword “global”. The keyword declares the local variable as a global, and provides access to the variable outside the function.

def some_func():
    global c
    c = 7
    
some_func()
print(c)

# Output 7

The reason for calling the function before the “print” statement is due to the way functions operate. Functions work only when called, so without calling the function, the “global” statement will not be applicable.

Multiple variables

In Python, multiple variables can assign multiple values at the same time, as well as assign multiple variables to the same value simultaneously. Similarly, values inside data storage types (also called collections) can be assigned to variables. This process is called unpacking.

Note that the number of values inside the collection must match the number of variables.

The following are examples of the above processes.

# Multiple variables to multiple values
a, b, c = 3, 7, 10

print(a)
print(b)
print(c)

# Output 3
# Output 7
# Output 10

Multiple variables to multiple values use commas for separation.

# Multiple variables to the same value
a = b = c = "Same value to all variables"

print(a)
print(b)
print(c)

# Output "Same value to all variables"
# Output "Same value to all variables"
# Output "Same value to all variables"

Multiple variables to the same value use equal signs for separation.

# Unpacking data storage types (collections)
numbers = (3, 7, 10)
a, b, c = numbers

print(a)
print(b)
print(c)

# Output 3
# Output 7
# Output 10

Unpacking uses commas for separation. The above example is with a tuple, but it will work with lists, sets, and dictionaries.

Note that with dictionaries, variables will assign to the values of the keys only.

Variable names

Naming variables is crucial for readability and understanding of Python code. Unfortunately, different developers use different styles. But consistency is key, as it can help immensely with following up code.

PEP 8 (here) is a Python document, which purpose is to offer guidelines on consistency and readability of Python code. There is an abundance of well-defined information regarding conventions and standards, such as naming variables, functions, parameters, classes, modules and so on. In addition, there is plenty of information on what to avoid, suggestions and what is prohibited.

Below are some of the most important rules about variable naming:

  • Variable names cannot start with a number
  • Variable names cannot be the same as built-in names
  • Variable names cannot contain symbols, except underscore (_)
  • Variable names are case sensitive (car and Car are two difference variables)

When it comes to variable names with more than one word, there are several methods of doing so.

The following are some of the most common usages.

# Single lowercase 
a

# Single uppercase
A

# Lowercase
somefunction

# Lowercase underscore
some_function

# Uppercase
SOMEFUNCTION

# Uppercase underscore
SOME_FUNCTION

# Capwords
SomeFunction

# Mixed Case
someFunction

# Capwords underscore
Some_Function

There are also some special forms, which apply in very specific scenarios.

# Left underscore
_some_function

# Right underscore
some_function_

# Left double underscore
__some_function

# Both side double underscore
__some_function__

Multiple imports

“Import” statement can load multiple packages simultaneously. Similarly, the keyword “from” can load multiple functions from a package.

Note: the PEP 8 guidelines always recommend separate module imports.

The following is an example of importing multiple modules.

# PEP 8 does not recommend this practice
import math, timeit, datetime

Below is an example of importing multiple functions.

from math import radians, pi, log

Similar to other programming languages, Python also allows loading everything inside a module – with the asterisk (*).

Please note that this is also discouraged by the PEP8 guidelines.

# PEP 8 does not recommend this practice
from datetime import *

Next: Python Intermediate Data Types

by AICorr Team

We are proud to offer our extensive knowledge to you, for free. The AICorr Team puts a lot of effort in researching, testing, and writing the content within the platform (aicorr.com). We hope that you learn and progress forward.