Variables and Functions

Part 2: Essential Concepts for New Python Users

Overview of variables and functions in Python.

This page covers variables and built-in functions. Be sure to check out the accompanying notebook for more experience.

NOTE: By design, Python does not return anything when a variable is assigned. So example cells here, where only variable assignment happens, will run without generating output. The variable is still saved in the runtime and can be accessed in following cells once it is set.

NoneReusability

Effective programming is built around reusability. Creating automated, reproducible code starts with variables and functions.

Variables

Variable assignment stores data in a named container. In Python, use = to assign values: x = 5. Variables are case-sensitive and can store numbers, strings, lists, and other datatypes. Variables in Python are mutable meaning the value can be modified.

The cells below set a variable x, print it, update it, and print it again.

Naming Variables

Variables names should be short and meaningful. Long names take time to type and can make code dense. Uninformative names can get confusing or accidentally overwritten.

Python rules for variable names include:

  • Letters, numbers, and underscores (_) only
  • Must start with a letter or underscore
  • Snake case is preferred (e.g., my_var)
  • No spaces, dashes, or dots
  • Can not be reserved Python word (e.g., if, return)

See the table below for more examples.

Example Valid? Reason
age βœ… Yes Starts with a letter, letters only
user_name βœ… Yes Letters + underscore are allowed
temperature2 βœ… Yes Digits allowed, but not at start
_hidden βœ… Yes Leading underscore allowed (often used for β€œprivate” values)
MAX_VALUE βœ… Yes Uppercase is allowed, often used for constants
2nd_value ❌ No Cannot start with a digit
user-name ❌ No Dash (-) not allowed
file.path ❌ No Dot (.) means β€œattribute,” not part of a name
my var ❌ No Spaces not allowed
return ❌ No return is a reserved Python keyword

Test Variable Names

Experiment with variable names below. The current syntax will result in a SyntaxError. Try to fix it, or explore the errors generated by other non-valid names like my.var, return, or 2nd_var.

Functions

Functions create reusable code which can simplify tasks, allow iterative runs, and can make your code much more powerful. Python comes with some builtin functions which will help demonstrate. The previous section already introduced print and type. The table below outlines some more of the most commonly used options.

Function Example Meaning Result
help help(len) Displays documentation about an object or function Shows usage info in console
len len([1, 2, 3]) Returns the number of items in an object 3
print print(β€˜Hello’) Outputs text or variables to the console Hello
type type(3.14) Returns the data type of an object <class β€˜float’>
min min(4, 7, 2) Returns the smallest of the given values 2
max max(4, 7, 2) Returns the largest of the given values 7
str str(123) Converts a value to a string β€˜123’
int int(3.9) Converts a value to an integer (truncates decimals) 3
float float(β€˜3.14’) Converts a value to a floating-point number 3.14

Function Exploration

In the first cell, a list is defined as my_list. After running it, use the second cell to explore more functions like: len, min, max, and type.

Next Steps

Keep going with iteration and flow control β†’.