- Hands-On Penetration Testing with Python
- Furqan Khan
- 488字
- 2021-07-02 14:13:46
Variables and keywords
Variables, as the name suggests, are placeholders that hold a value. A Python variable is nothing but a name that can hold a user-defined value during the scope of a Python program or script. If we compare Python variables to other conventional languages, such as C, C++, Java, and so on, we will see that they are a little bit different. In the other languages, we have to associate a data type with the name of the variable. For example, to declare an integer in C or Java, we have to declare it as int a=2, and the compiler will immediately reserve two bytes of memory in C and four bytes in Java. It would then name the memory location as a, which is to be referenced from the program with the value 2 stored in it. Python, however, is a dynamically typed language, which means that we do not need to associate a data type with the variable that we will declare or use in our program.
A typical Python declaration of an integer might look like a=20. This simply creates a variable named a and places the value 20 in it. Even if we change the value in the next line to be a="hello world", it would associate the string hello world with the variable a. Let's see that in action on the Python Terminal, as follows:

To use the Python Terminal, simply type the python3 command in your Terminal prompt. Let's think about how this works. Take a look at the following diagram, which compares statically typed languages with dynamically typed languages:


As you can see in the preceding diagrams, in the case of Python, the variable actually holds a reference to the actual object. Every time the value is changed, a new object is created in memory and the variable points toward this new object. The previous object is claimed by the garbage collector.
Having discussed that Python is a dynamically typed language, we must not confuse it with a weakly typed one. Though Python is dynamically typed, it is also a strongly typed language, just like Java, C, or C++.
In the following example, we declare a variable, a, of string type and a variable, b, of integer type:

When we carry out the operation c=a+b, what might happen in a weakly typed language is that the integer value of b would be typecasted to a string, and the result that was stored in variable c would have been hello world22. However, because Python is strongly typed, the function adheres to the type that is associated with the variable. We need to make the conversion explicitly to carry out any operations of this kind.
Let's take a look at the following example to understand what it means to be a strongly typed language; we explicitly change the type of variable b and typecast it to a string type at runtime:

- C語言程序設計實踐教程(第2版)
- Unity Virtual Reality Projects
- jQuery從入門到精通 (軟件開發視頻大講堂)
- 深入理解Java7:核心技術與最佳實踐
- 快速念咒:MySQL入門指南與進階實戰
- Windows Phone 7.5:Building Location-aware Applications
- Django實戰:Python Web典型模塊與項目開發
- uni-app跨平臺開發與應用從入門到實踐
- 硬件產品設計與開發:從原型到交付
- Tableau Desktop可視化高級應用
- Android移動應用開發項目教程
- 跟戴銘學iOS編程:理順核心知識點
- 邊玩邊學Scratch3.0少兒趣味編程
- Java從入門到精通(視頻實戰版)
- INSTANT EaselJS Starter