Welcome back! This is the fifth lesson in our Python programming course. In the last chapter, we discussed how string data types are used in Python. In this tutorial, we’re going to discuss variables in python and the rules for naming them in Python. In addition, you'll learn the fundamentals of working with numbers and strings.
All programming languages use variables as a fundamental building block of their language. It is the allocation of memory that is dedicated to data storage and manipulation. Variables are program elements that keep track of data. The following is an example of a variable.
x = 100
It's called x in the diagram below, and it has a value of 100 in it. In this case, the variable name is x, and the data it contains is the number.
For a variable, its data type is just the type of data it contains.
If a variable has a certain data type, it can be used with any mathematical, relational, or logical operation without resulting in an error; this is known as the data type in programming. There are many different types of data, such as strings and integers, that are used to categorize different kinds of information.
As a Python programmer, we don't have to do anything to free up space. Garbage collection handles this.
There are two types of memory:
Stack memory is used to hold all functions and their calling references. The lifo rule governs how a stack operates. Continuous blocks make up the stack memory. When a function is called, the variables it returns are kept in the program's call stack, where they can be freed upon function completion and the program's exit successfully.
Heap memory refers to the memory allocated at run time when each instruction is performed. A heap is used to store global variables, which can be shared by all functions in the program, rather than the local variables defined within a function. Here, x = 23 is an example.
Integers, floats, and complex numbers are all available as numeric types in Python. In this tutorial, we'll learn about integers because we'll be using them to show how variables work.
For simplicity's sake, an integer is a number that does not include a decimal point or fraction. They might be both positive and negative at the same time. For example, the numbers 1, 2, 3, 500, and 10000000.
The int class contains all integer variables. The class name can be found using the type () method, as illustrated in the example below.
A variable refers to an item's specific name. Instead of having to declare or specify a variable like in other programming languages, Python simply uses the equals (=) sign to create a variable and assign a value.
As stated, "n is allocated the value of 300" in the example above. You can then use n in a statement or expression, and its actual value will be substituted for n.
Just like with a literal value, variables can be shown directly from the interpreter prompt in a REPL session, without the usage of print ().
In the future, if you use n again and alter its value, the new value will be used:
Chained assignment in Python allows the same value to be assigned to many variables at the same time:
The above-chained assignment gives the variables a, b, and c the value 300 at once.
Python has two types of variables: global variables and local variables. To utilize the variable in other parts of your program or module, you need to declare it as a global one. It is common practice in Python to use local variables when creating new variables.
Using the following program, you can see how Python variables function by comparing the local and global variables.
You can utilize a global variable in a function in Python if you include the global keyword in the variable definition.
Variables are statically typed in many programming languages. A variable's data type is specified when it is created, and any value that is assigned to it must always be of the same type.
However, Python variables are exempt from this rule. It is possible in Python to give a variable a value of one type and then change it to another type later on:
This is an important subject in many programming languages, but the answer in Python is a little different.
Since Python is a highly object-oriented language, every piece of data in a program is an object of some type or class. This will be brought up again and again in the course of these tutorials.
Take a look at the following coding:
Translators are instructed to perform the following actions when provided with a statement print (300).
As you can see, the built-in type () function creates an integer object.
An object can be referenced by a Python variable's symbolic name, which serves as a pointer or reference. After assigning a variable name to an object, you can use that name to refer to the object. However, the object itself still contains the data. For instance,
An integer object with the value of 300 is created and the variable n is assigned as an identifier.
n is a pointer to an integer object, and the following code confirms that:
Then consider the following assertion:
What happens if it is executed? A new object is not created. Since n points to the same item, m points to a new symbolic name or reference.
Next, you may try something like this:
Python now constructs a new integer object named m, which has a value of 400.
Finally, let's consider that the following statement is executed:
As a result, Python generates a string object named "foo" and uses that as a pointer in n.
The integer object 300 is no longer mentioned anywhere. There is no way to get in touch with it.
The lifespan of an item will be mentioned at various points in this series of tutorials. When an item is first created, at least one reference to it is made, which marks the beginning of its life. A reference to an object can be added or destroyed at any moment during its lifetime, as you saw in the example above. As long as there is at least one reference to the thing, it lives on.
Objects are no longer accessible when the number of references to them reaches zero. Its time had come to an end at that moment. The allocated RAM allocated to Python will eventually be reclaimed so that it can be utilized for something else. Garbage collection, as it's called in computer jargon, is a very simple way to manage your own memory.
Every Python object is assigned a unique identifier number when it is created. No two objects will ever share an identifier, even if their lives overlap for a period of time. It is not possible to utilize the same identifier for an object after its reference count reaches zero and it is garbage collected.
A Python object's id can be obtained using the id () function, which is part of the standard library. Id () can be used to verify that two variables are actually pointing at one another:
Even after an assignment such as this, which results in the same number being returned for id(n), it is clear that these two numbers point to the same thing. As soon as m is reassigned to the value 400, they no longer point to the same item, but rather to different things, take into account the following:
M and n are both 30-valued integers in this example. The id(m) and id(n) are identical in this example, While running the program, the interpreter makes use of previously created objects for numbers in the [-5, 256] range. As a result, if you assign different integer values in this range to different variables, they will all refer to the same thing.
Python variables can also be deleted by use of function del "variable name". Python's error "variable name is not defined" indicates that you've erased the variable in the following example of the language's delete variable function.
Test the concatenation of several data types such as string and integer. The number "99" will be added to the word "Guru," for example, A Type Error will arise if the number is not expressed as a string when declaring variables in Python. This differs from Java, which does not require the number to be declared as a string.
You will obtain an undefined result if you run the following code.
a="Guru"
b = 99
print a+b
Use print(a+str(b)) to concatenate both a and b into a string.
This is the process of changing one data type to another. Data type conversion is necessary in some scenarios. For instance, you may want to combine two numbers where one of the variables' existing values is an integer and the other is a string. Python typecasting is required before addition, as it must transform a string's data type to an integer.
Type casting in Python can be done using a variety of functions.
Data types in Python are implicitly converted by the Python interpreter, which means that the user is not required to intervene in the process. See the examples below for a better understanding of the subject.
Congratulations! Your perseverance has paid off. The focus of this session was on Python variables, object references, and identity, as well as the naming conventions for Python identifiers and a brief discussion of integers. Let’s meet in our next tutorial as we discuss about Python data types and how they're put to use.
The rapid developments that have been achieved in the Light Emitting Diode industry more so in the area of high-power LEDs, have brought up a challenge on heat dissipation. The LEDs are always mounted on the printed circuit boards and they might end up bringing a lot of problems, especially on the heat generated from them. Without proper laid down structures to dissipate the excess heat will end up damaging the board. To solve this, designers have chosen to implement the use of metal core PCBs.
Metal core PCB boards are special types of PCBs that have a metallic layer that is made up of copper or aluminum. This metallic layer is what gives it this name.
There are many online PCB companies offering Metal Core PCB manufacturing. We are going to take the example of JLCPCB, a China-based online PCB Fabrication House. JLCPCB offers competitive rates and provides excellent results and is considered one of the best PCB manufacturers. You can place a PCB order on JLCPCB's official website.
So, it's quite easy to order for manufacturing of Metal Core PCB on JLCPCB.
When you compare the metal core PCB with other traditional standard PCBs you will realize that it has special layers. The total number of these layers on the PCB will be determined by the total number of conductive layers that you really need. They can be of single or multiple conducting layers. The layers can be categorized into three types
There are three major categories of the MCPCB as discussed below;
This type of metal-core PCB has the copper traces printed on one side of the board and the board comprises of the following;
The single-sided board also has a dielectric layer that is sandwiched between the copper and the metal core layer.
It comes with over two layers hence having a structure that looks like the FR-4 type of PCB materials.
Let us have a look at the steps that can be followed in the design of the metal core printed circuit boards as listed below;
After this step, you now need to confirm the digital image that you have with the original Gerber files from the designer using an inspection laser to confirm if you have done the right thing.
After the confirmation that you have done the right thing, now the design can be moved to the final process of the design. This final step involves the unpacking of the PCB layers accordingly. We are supposed to locate the drill points and this can be done by the use of the x-ray locator.
Now the board is supposed to undergo the process of plating and deposition of copper where the whole PCB is electroplated with the copper layer before the board is taken through the final process of v-scoring and profiling.
The PCBs that are made out of aluminum offer very smart heat dissipation and a good heat transfer mechanism. Aluminum PCBs are very light in weight and are used in LED lighting applications, electronic communication and audio frequency equipment. Listed below in the characteristics of the aluminum substrate;
PCBs made out of the copper core have better performance than those made out of aluminum. But aluminum is preferred to copper by most clients because copper is more expensive. Another disadvantage of copper core over aluminum is that copper boards are heavier and involve a tough process of machining. Copper has a higher rate of corrosion as compared to the aluminum core.
This type of board finds great use in the field of LED technology. Some of the applications are listed below;