Continuing our Python journey deeper into this dynamo of a programming language we will take a leap into variables and types. Variables are one of the most fundamental concepts and exist in pretty much all programming languages. Variables are by no means specific to just the language of Python and they will certainly be required in most of your programming escapades. Python provides a simple and dynamic way to create variables in your code, yet it maintains a powerful type categorisation system to ensure safe operations with your data.

Transcript

Hey gang! Tim here from Core Electronics, continuing our Python journey deeper into this dynamo of a programming language we're going to take a leap into variables and types.

Variables are one of the most fundamental concepts and exists pretty much in all programming languages. Variables are by no means specific to just the language of Python and they will certainly be required in most of your programming escapades, Python provides a simple and dynamic way to create variables in your code yet it maintains a powerful type categorization system which ensures safe operations with your data. This conversation will allow you to have a complete understanding of what a variable is inside the Python programming language, furthermore it will clarify every variable type and outline how they can be useful in your coding ventures, variables are just a method to temporarily store data in a computer's memory.

A variable is created the moment you first assign a value to it, the equal sign is used to assign this value to a label or an identifier which creates in Python a variable, so imagine variables as a box filled with books and a label is slapped onto the outside of this box, these books then can be changed or replaced or altered at any point, so this label or identifier allows you to access the data within the variable anywhere within your program. It is best practice to give your identifier the name of the variable and a name that makes sense. Python is a case sensitive language that means if you type “X = 1” and “x = 2” into your code Python will record and store two unique variables into the memory, every variable in Python must have an identifier which can consist of letters, numbers and/or the underscore character, variables do not need to be declared with any particular type, this is worth noting and they can even have their type changed after the variable has been set.

I'll quickly demonstrate jumping into this computer, you can see I have the Python programming window open and if I was to type “X” the identifier, “= 2” just like that when we run this code X = 2, “X” is the identifier, “X = 2” is the variable, “2” is the data of that variable. So like I said before when it comes to naming variables don't be afraid of using longhand and avoiding the abbreviation as it will be easier to figure out what's going on after you've spent a long time away from your computer screen. When writing longhand identifiers you can also use underscores to separate words, now there's lots of different types of variables within Python, different data types allow you to do different things to the data whilst maintaining that safe operation, Python will track the value of a variable letting you access it via the identifier and it will also track the type assigned to that variable.

Now worth noting, a thing that pops up regularly is where the data is “mutable” or “immutable” in object orientated and functional programming languages such as Python, an immutable object is an object which state cannot be modified after is created whereas a mutable object is one that can be changed. Object and variable are effectively interchangeable words now it's useful to know that variables do not need to be manually declared with any kind of variable type, so to figure out what the type of a variable is you can use the “type bracket bracket function” in the Python shell window. I’ll demonstrate this right here, we ran this code it will tell us exactly what “X” is, this function is installed by default into Python now you can also check out the online guide where you can see a write up and that lists every single python variable. This is an excellent place for reference also open is a Python programming window which has several variables, in fact every single variable inside Python when typing variables you don't need to use a specific type function but in this particular case it's been done for ease of understanding.

So starting from “A” this here is a string literal type, it's the method to get text into your code in Python as well as numerous other programming languages a string is a sequence of characters or single character, some characters and symbols have special meanings which can cause processes to run when the code is executed in Python this can be a problem if you don't want that particular process to happen and instead just want to display the character inside the string. To use these characters in a string literal without triggering a special process use the backslash followed directly by the desired character, an example of this can be seen in the written up page, down here it's impossible to write a quotation mark without using this forward slash and when we print this, you can now see all of this has been written including the single quote.

This is a Boolean variable type in computer science the Boolean variable type is a type which has one of two possible values, the intent is to represent the truth values of logic this simply means determining whether a proposed statement is truthful or a falsity in programming you often need to know if an expression is true or false in Python the recorded data for this fact would be a Boolean type, make sure you use capital letters when you type “true” or when you type “false” now we're on to the numeric variable types.

This one right here is an integer variable type, for a variable to be an integer, the data must be a single whole number it can be of an unlimited length in Python, furthermore it must be a positive or negative number and it must not have decimal or fractional components. Python and many other programming languages will store automatically numbers as integers so long as it fits this above criteria, ergo you don't need to write “integer bracket bracket” function to store integers so now “D” this is a float variable type it must be a positive or negative number and contain a decimal or fractional component it can be of unlimited length, within Python the character “E” is used to denote the power of 10 which is also a float, moving on we're up to “E” this is a complex number variable these are useful, as an abstract quality which can be used in calculations which result in physically meaningful solutions, complex numbers are written in the form “A + BJ” where “A” and “B” are real numbers and “J” is the square root of “-1” now this is not used very often in Python programs but they will start coming up if you start diving into Fourier transforms or fractals such as the Mandelbrot set.

Moving on to a different class of variables, we're now into the sequence starting off with the first one, a “list” this is a “list” type variable a variable will be a “list” type if the data is a collection which is both audit and changeable, ergo mutable lists are very similar to arrays, an array is an arrangement of objects pitches or numbers and columns and rows in Python “list” type data can contain any type and combination of variables such as a string numerical and Boolean variables they can also contain as many variable members as you wish, within Python lists can be written with a square bracket saving you from typing “list bracket bracket”. “G” is a “tuple” now a variable will be a “tuple” type if the data is a collection, which is both ordered and unchangeable it must also allow duplicate members, in Python “tuples” are written with round brackets and cannot be iterated over. Once a “tuple” is created you cannot change its values so “tuples” are immutable, there is a work around this though because you can also change the type of data so you could turn a “tuple” into a “list” change it and then turn that change “list” back into a “tuple”.

“H” is a “range” type variable variables of this type are created using the “range bracket bracket” function the range function is a good tool that produces iterables, that yield arithmetic progressions the results will always be integer numbers there are a number of examples on how to use this function on the online writer, which I could jump to real quick, right here so you can determine what the jump size is you can determine where it goes from 0 to 40 it can be negative numbers there's a lot of options with range.

We're now up to the “set” categories of variables so “I” so this is a “set” type variable a variable will be a “set” type if the data is a collection which is both unorganized, unordered and unindexed, it must also not allow duplicate members therefore every single piece of data inside a “set” is unique, in Python sets are written with a curly bracket and this can also be iterated over, this means that the data is mutable and thus it can be modified after it's created. A frozen set “J” is simply an immutable version of a Python set thus a variable will be a frozen “set” type if the data is called is a collection which is immutable, unordered and unindexed. Thus, it must not allow duplicate members therefore every piece of data inside a frozen “set” is unique and the data type is immutable and thus cannot be modified after it's created.

Moving on to a mapping category, “K” right here, this is a “dictionary” variable which is a collection of data which is unordered, changeable and indexed, it also will not have any duplicate members in Python dictionaries are written with a curly bracket with elements separated by commas, so each member will have a key and a value. This is expressed by the paired key value the values can be of any data type and can repeat keys must be immutable which means unchangeable and must be unique, this is important to allow indexing of data in the “dictionary” variable, this data type is often referred to as associated memories or associative arrays in other programming languages.

All the next variable types have to do with binary, it's these guys, these variable types are Python attempting to give the user a low level code functionality this is for people who want their code to run at a higher speed. An example of low-level programming would be like “C” or like machine code so binary types remove some of the abstraction between what the computer is doing versus what the human written code is. A byte is a unit of digital information that most commonly consists of 8 bits, the byte originally was the number of bits used to encode a single character of text in a computer, for this reason it is the smallest addressable unit of memory in almost all computers. I will quickly go over these and if you want more in-depth knowledge you should check out the online written guide.

So starting with “L” the variable “L”  bytes, this is a “byte” type, a variable is a “byte” type if the data is immutable thus unchangeable and there's a sequence of single bytes, this sequence will be integers in the range of 0 to 255. Looking at “M” this is a “byte array”, bytes array are simply a mutable version of a “byte set” which is what's above. Now “memory view” now this is a variable type that is very rarely used but is a superb example of Python giving the user the potential for low level functionality, what this will let you do is get it lets information really fast and not have to copy the entire file to get the information desired, only two variable types will work with this function and those are the “bytes” and the “bytes array”.

I hope this has granted you significantly more, I often find when it comes to programming you can get caught off guard with just new stuff being thrown at you from left and right, going through all these variables should prevent any surprises in the future and just allow you to be cemented with complete and overarching knowledge on the Python variables, so I hope this has been helpful and until next time stay cosy!

Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.