Table of Contents
Variables are a way to store a piece of data. Variables are a fundamental part of Python, as they are with any programming language. Variables have two components to them, the name and its value. Let's start by creating one.
Declaration
Declaring a variable is incredibly simple in Python. It looks like this:
PYTHONx = 1
y = "sabe.io"
print(x)
print(y)
BASH1
sabe.io
Variables in Python are created by simply setting their values, that's it. Unlike in other programming languages, you do not need to specify the type of variable, it is done automatically for you.
Variable Naming Rules
Here are the rules for what are valid Python variable names:
- The variable name must begin with a letter or underscore
- The variable name cannot begin with a number or symbol
- Throughout the entire variable name, it can only contain alpha-numeric characters with the only exception being underscores
With that in mind, here are some examples of valid Python variable names:
PYTHONapples
_apples
_apples_
And here are examples of invalid Python variable names:
PYTHON1apples
app les
$apples
%apples
Data Types
Python supports five standard data types. We've already seen two of them already, Numbers and String. Here is the entire list:
- Numbers
- String
- List
- Tuple
- Dictionary
It is useful to know that you can get the type of a variable by using the type
method, like so:
PYTHONx = 1
y = "sabe.io"
print(type(x))
print(type(y))
BASH<class 'int'>
<class 'str'>
We'll get into lists, tuples, and dictionaries in later lessons.
- Getting Started with Express
- How to Serve Static Files with Nginx and Docker
- How to deploy a PHP app using Docker
- How to deploy a Node app using Docker
- Using Puppeteer and Jest for End-to-End Testing
- How to Scrape the Web using Node.js and Puppeteer
- Getting User Location using JavaScript's Geolocation API
- Creating a Twitter bot with Node.js
- Building a Real-Time Note-Taking App with Vue and Firebase
- Getting Started with React
- Setting Up Stylus CSS Preprocessor
- How To Create a Modal Popup Box with CSS and JavaScript