Variables

From Spark Makerspace
Revision as of 05:16, 15 January 2022 by Jjfs85 (talk | contribs) (Created page with "<span id="variables"></span> = 2. Variables = <span id="summary"></span> == Summary == Ok so now you can do simple calculations in Python, but what if you want to store and...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

2. Variables

Summary

Ok so now you can do simple calculations in Python, but what if you want to store and recall information? Well….

What is a Variable?

A variable is used to store information that can be referenced later

Declaring a Python Variable

Try this:

<syntaxhighlight lang="python">>>> result</syntaxhighlight> What happens?

What you see is how Python shows you an error. The error in this case is that the variable “result” hasn’t been declared. You declare a variable by assigning a value to a name. Like this:

<syntaxhighlight lang="python">>>> result = 3 * 5 >>> result 15 >>> result - result/2 7.5 >>> </syntaxhighlight>

Variable Naming Rules

Can I name a variable Steve? fr3d? 123? 1var? _hi_? Some work and some don’t. There are some rules for what makes a valid variable name.

Valid variable names can only contain: * Lowercase and uppercase letters: a-z and A-Z * Numbers: 0-9 * Underscores: _

Additionally, there are these two rules: * Variable names must start with a letter or the underscore character, and can not start with a number. * Names are case-sensitive

Good Practices for Variable Naming

Make your variable names as descriptive of what you’re storing as possible. You’ll thank yourself later when picking up an old project. It also helps other people to more easily read and understand your code. Python variable name convention is to use all lowercase with underscores (_) separating word. For example: shopping_cart_total.

Datatypes

So far we’ve been working with numbers. Numbers come in a few flavors in Python: int which is short for integer, and float are the two most common. An int holds whole numbers like -4, 15, and 42069. A float holds decimals like 4.0, 7.205, and 69.420. Unlike other programming languages, we don’t have to explicitly tell Python variable types. It’s smart enough to ensure that variables will always hold whatever is assigned to them.

If you ever want to know what datatype a variable is holding, use something called the “type” function.

<syntaxhighlight lang="python">>>> sum_total = 1 + 2 + 1.5 >>> sum_total 3.5 >>> type(sum_total) <class 'float'></syntaxhighlight> Next we’ll learn all about another datatype for text!