Python for Hacking | Part 2 | concept of data types, variables, constants, and keywords

Python for Hacking | Part 2 | concept of data types, variables, constants, and keywords

What is a data type?

Data-type of a value means the type of value being stored in the memory. For example, 4 is a value of the data-type int (integer), 4.0 is a value of the data-type float (decimal), 'Hello World' is a value of the data-type character (string).

What is a Variable?

A variable is an entity in Python which can change its value. A value assigned to a variable can be changed later during the execution of the program. For example, a = 15 is a variable which contains the value 15 of the data-type integer. Similarly, subject = 'Computer Science' is a variable which contains a string of characters.

How to declare a Variable in Python?

To declare a variable and store a value in it, just type the name of the variable and assign a value to it by following a '=' sign after the variable name. As you can see in the below image, we have declared three variables of the type integer, float, and string. You don't have to write the data-type of the variable along with the name of the variable while declaring a variable in Python, as in the case of programming languages like C/C++ et cetera. Python interpreter is smart enough to know the exact data-type of the variable just by its value.

NOTE: While declaring a variable of data type int and float, the value assigned to them after the '=' sign should not be in '', because a value written inside ' ', Python interpreter will treat it as a String. Similarly, to declare a variable containing a pair of string, the value assigned to the variable must be inside ' ' or " ". 

REMEMBER: Python is a case-sensitive language means, 'Python' and 'python' have different meanings in Python. Python interpreter treats them as two different entities.

What is a Constant?

A constant in Python is an entity which cannot change its value. A constant once declared, cannot change its value during the entire duration of the program.


How to declare a Constant in Python?

You cannot use a Constant in Python just like a Variable instead, you have to first declare a Constant value inside a module and then, you have to import that module so as to use that Constant value in your program. 

To declare a constant value, first of all, make a Python module. A module is nothing but a file consisting of Python code.

To declare a module, open your Python IDLE terminal, and then click on 'File' tab in the tab menu. And then, click on 'New File' option in the drop-down menu. Another option to create a new file in Python is to simultaneously press 'Ctrl+N' key. A new window will open on your screen and you can now type your Python code inside that file (see in the image below).



Write the following code inside the new window as in the image:


Then save this file by clicking on 'Save As' inside the File menu. You can give any name to this file. For the sake of this tutorial, I am giving the name as 'constant' to this file.

Create a new file by clicking on New File and import the constant.py file you just created by typing the following code inside this file as shown in the below image:


Now, save this file with the name 'Main.py'. Don't change the default installation path of the program or if you want to change the default installation path, make sure that both the 'constant.py'
and 'Main.py' should be in the same folder otherwise, Python interpreter will not be able to import Constant module and you'll get an error.

Don't panic if you're not able to understand the code now, as I will discuss all the code written in this file in detail later. For the time being, focus only on the result of the program.

Now, run this file by pressing the F5 key or by clicking on the 'run module' option inside the RUN tab menu. You will get the following OUTPUT on your screen:


Remember one thing, write the Constants inside the module file in CAPITAL letters.



How to assign a name to a Variable and Constants in Python?

Although you can assign any name to a Variable in Python, generally the following convention is followed while assigning a name to a Variable in Python:
  • The first letter of the name must be an alphabetical letter. You cannot start the Variable in Python with a number. For example, a Variable with the name 4eet is not valid in Python whereas, a Variable with the name e4et is a valid one. 
  • You must use Camel-case notation to write a name of the variable containing more than two words. For example, myName, theVariable et cetera. The first letter of the first word is generally written in lower case whereas, the first letter of the second word must begin with an Uppercase.
  • You can only use alpha-numeric characters ((a-z) or (A-Z) or (0-9)) while naming a  Variable in Python. 
  • You cannot use special symbols like #,$,!,@,& et cetera while naming a Variable in Python.
  • You can start the name of the variable with a '_'. For example, '_constant' is a  valid name in Python.
  • You can separate the two words in the variable name with a '_'. For example, 'my_variable' is a valid name in Python.

What are keywords?

Keywords in Python are reserved words which you cannot use as ordinary variables or constants. They are reserved words having predefined meaning and usage. For example, print is a keyword in Python which is used to print a value on the screen and cannot be used to store a value. 
For example, if you will try to store 7 in print or print = 7, you will get an error from the Python interpreter.

Refer to this link, to see the complete list of keywords in Python.


https://www.ethicalhackingonline.ooo/2018/09/python-for-hacking-part-3-what-are-functions.html

         
                                                                                                                                                
Share:

No comments:

Post a Comment

Email Subscription

Enter your email address:

Delivered by FeedBurner

Wikipedia

Search results

Translate

Contact Form

Name

Email *

Message *

Featured Post

How hackers can hack your car?

How hackers can hack your car? For a considerable length of time, AUTOMAKERS and programmers have thought about a smart assault that ...

Search This Blog

Archive

Subscribe

Comment

Popular Posts

Recent Posts