Python for Hacking | Part 4 | concept of conditional statements and recursion

Python for Hacking | Part 4 | concept of conditional statements and recursion

Python for Hacking | Part 4 | concept of conditional statements and recursion

In this post, we are going to discuss the concept of Conditional statements and Recursion in Python. We will be going to discuss both the topics in detail and will know exactly how and where to use them properly.


Conditional statements are basically used to check whether a condition is true or not. If a statement is true, the piece of code behind the true part is executed and if false, the part of the program after the false statement is executed.

Conditional statements are very useful when we have a set of different conditions and we have to check that which of those statements are true and they change the behavior of the program accordingly.

The simplest form is the if statement:

x =2
if x > 1:
  print('x is greater than 1')

In the above example, we are checking the condition that whether the value of x is greater than 1 or not. If the value of x is greater than 1, we will get 'x is greater than 1' on our screen. If it's not greater than 1, the program will end without doing anything. Since in the above example, we have the value of x as 2 which is greater than 1 so, we will get the print statement on our screen.

Python for Hacking | Part 4 | concept of conditional statements and recursion

NOTE: In the above example, see the logical expression (x>1). Python uses the following logical conditions which are the same as in the Mathematics:
  • equal to: a == b 
  • not equal to: a!=b
  • less than: a < b
  • less than or equal to: a <= b
  • greater than: a > b
  • greater than or equal to: a>=b

Now, let's discuss each and every logical condition in detail:

1. equal to: 'a == b'

This operator '==' is used to check whether one entity is equal to another entity or not. Don't get confused with the '=' operator which is generally used in mathematics to check the equality condition.
In Python or many another programming languages, the equality condition is checked by the double equal sign '=='. Single equal sign '=' in Python is used to assign a value to an entity (for eg. a variable).
If you used 'a = b' to check the equality condition, the Python interpreter will give you an error.

2. not equal to: 'a != b'

This operator '!=' is used to check whether an entity is not equal to another entity. If an entity is not equal to another entity, then the part of the code followed by the 'true' condition will be followed. It is the opposite of the 'equal to' operator.

3. less than: 'a < b'

This operator '<' is used to check whether an entity is smaller than another entity. If the entity is smaller then, the part of the code which is meant to be run for the true case will be executed.

4. less than or equal to: 'a <= b'

This operator '<=' is used to check whether an operator is less than or equal to another operator or not. The true part of the code, in this case, will be executed when the first variable is either equal to or less than the second operator. Remember that there is no such thing as equal to or less than '=<'. If you will use it, you will get an error.

5. greater than: 'a > b'

This operator '>' is used to check whether an entity is greater than another entity or not. If the entity is greater then, the part of the code which is meant to be run for the true case will be executed.

6. greater than or equal to: 'a >= b'

This operator '>=' is used to check whether an operator is greater than or equal to another operator or not. The true part of the code, in this case, will be executed when the first variable is either equal to or greater than the second operator. Remember that there is no such thing as equal to or greater than '=>'. If you will use it, you will get an error.



elif, else

* else

'else' is used when we have more than one possibilities and we have to determine which part of the code will be going to execute depending on the given condition to be tested.

for example,

x = 5
if x>1:
  print('x is greater than 1')
else:
  print('x is either equal or lesser than 1')

In the above example, we have initialized x to 5. Then we have to check whether x is greater than 1 or not. There are two possibilities of this event that either x will be less than 1 or x will be greater than 1, so we have used the keyword 'else' along with 'if' statement to check this condition. 

* Elif 

'elif' is used when we have more than two possibilities for a given condition and we need more than two branches.

For example, 

x=2
y=4
if x < y:
  print('x is less than y')
elif x >y:
  print('x is greater than y')
else:
  print('x and y are equal')

In the above program, we have initialized x to 2 and y to 4. Here, we have to check three conditions. Remember, in order to check two conditions we can easily do it by using the 'else' keyword but in order to check more than three conditions, we will need a new keyword called 'elif'. 'elif' is basically an abbreviation for else-if.

In the above example, we have to check whether x is greater or smaller or equal to y. That's why we have used the keyword 'elif'. Look carefully, the use of 'elif' in the above example. We can use 'elif' any number of times in the program depending on the number of possibilities of the condition to be tested. But, remember that the condition must be started with an 'if' keyword and must end with an 'else' keyword. You can use any number of 'elif' keyword between them. The statements in the example look like a chain of if-else-if statements, that's why they are also known as chained conditionals


Nested conditionals

One conditional statement can be nested within the other. It means we can easily start one conditional statement inside a conditional. This is called nesting or nesting conditional statements. 

For example,

x = 1
y = 2
if x == y:
  print('X is equal to Y')
else:
  if x > y:
    print('X is greater than Y')
  else:
    prinr('X is less than Y')

In the above example, we have nested one 'if-else' conditional statement inside an 'if' conditional statement. 


Recursion 

We have already discussed and learned about the Functions and we talked about declaring, defining and calling them in a program. We also know that a function can call another function in a program.

But, do you know that a Function can call itself any number of time in the program. This property of a Function to call itself in a program is called Recursion. 

For example, 

def printN(n):
  if n == 0:
    print('Terminate')
  else:
    print(n)
    printN(n-1)

printN(2)




In the above example, we have done recursion by calling the function printN() within the function itself (see in the second last line).

Conclusion 

In this post, we have learned about conditional statements, how to use them, different types of conditional statements and their usage. We have also learned about Recursion and it's practical usage with the help of an example. 

If you have any doubt, you can ask in the comment section.

Please share this article with your friends.

Share:

Python for Hacking | Part 3 | concept of Functions

Python for Hacking | Part 3 | concept of Functions

In this post of the series of Python for Hacking, we are now going to discuss the concept of Functions and how to declare them, call them and use them in a program with the help of an example.

So, let's get started...



3.1 What is a Function?

A Function in Python is simply a piece of code which runs only when that Function is called. A function helps in reducing the effort to type the same piece of code over and over again. This comes very handily when a program contains several hundred or thousands of lines of code and there is a need to type the same piece of code again and again. In that case, we will just define a function containing a piece of code and call that Function whenever, wherever (I love that song Lol) needed.

3.2 How to create and call a Function in Python?

3.2.1 Creating a Function in Python

In Python, a Function is declared using the def keyword followed by the name of the function. For example, if we want to create a very simple Function which when called will print something on the screen, it will be created as follows:

def some_function():
  print('hello python!')
  print('I love Python!')


[If you do not know how to create a file in Python, visit this link]

Note the colon ( : ) after the name of the function. It is compulsory to put a colon after the name of the function. These brackets ( ) after the name of the function will contain arguments which will be passed to the Function when it will be called. We will discuss arguments in the coming section.

Notice the spacing before the print('hello Python!') and print('I love Python!') commands above. It is necessary to put some spacing before each and every line inside a function. This spacing inside the function is necessary to be done because of this spacing before each and every line of the code, it will tell the Python interpreter that these lines of code are not the part of the actual program, rather it's the part of the function. If you'll not do the spacing before each and every line inside the Function, Python interpreter will return an error. This spacing before each line of the code inside the function is called indentation.



3.2.2 Calling a Python function

To call a function, just type the name of the function outside the function declaration and run the program. 

def some_function():
  print('hello python!')
  print('I love Python!')
  
some_function()

You'll get the following output:

3.3 What are arguments/parameters?

Argument or a parameter is the information/value which is passed to the function and is then used inside the function wherever required. 

Arguments are written inside the parenthesis in the function call. You can pass as many arguments as you want to a function. Just separate the arguments by a comma (,).

For example, if you want to pass first_name and last_name as the arguments to a function printName to print a name on the screen, then the program will be as follows:

def printName(first_name,last_name):
  print(first_name + last_name)

printName('Dishant',' Sharma')



In the above example, we are passing arguments as 'Dishant' and 'Sharma' of data type string to the function printName. Both the arguments inside the parenthesis in the function call statement are in ' ' because they are of the data type string and if you do not put them inside the ' ', Python interpreter will treat them as integer and it will give you an error because Dishant and Sharma are strings and you're trying to add them as integers.

When we will run this program, we will get the following output on our screen as shown in the image:



3.4 Default argument value

What if we forget to pass the argument/s while calling a function and the result of the function depends on it?

Well, we also have a solution for that problem in Python. If we will forget to pass arguments to a function, Python interpreter will take default argument values and will pass it to the function. That default value which interpreter passes to the function is called the Default Argument Value.

For example, 
def country(country_name= 'United States'):
   print('I live in '+country_name)

country() 


When we will run this program, we will get the following output as shown in the image below:


So basically in the above program, we didn't pass the argument to the function instead, the Python interpreter takes the default value of the argument which we defined to the argument while defining the function.

3.5 Returning a value

To return a value in Python, we basically use a keyword called return. For example, 
def sum(x):
   return x+2

sum(2)  


When we will run this program, we will get the following output on the screen:




In the above example, the function returns a value equal to 4.

Hope, you have understood this tutorial. If you had any doubt, please do ask in the comment section. Please share this article with your friends also.


Share:

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:

Python for Hacking | Part 1 | what is python | installing python in the system | first program in Python

Python for Hacking | Part 1 | what is python | installing python in the system | first program in python




Python is a high-level programming language. A high-level programming language is the one which can be easily understood by human or in other words, a high-level programming language is much like the English language. A low-level programming language is the one which is used by computing systems (like a computer or laptop) to follow an instruction given by the user of that system. For example, sending a mail, saving a document, performing a calculation etc. Firstly, a program is written into a high-level programming language and then, it is converted into low-language or simply machine-understandable form by the means of a piece of a software called an interpreter or a compiler. Python is an interpreted language, so an interpreter is used to convert the program from high-language form to low-language form in case of Python. A compiler converts the entire code into a machine-understandable form in one go, whereas, an interpreter converts the code into a machine-understandable form by converting through line-by-line.

In this Python for Hacking series, we will first learn the basics of Python and then we will move forward to learn to make hacking scripts written in Python.

So let's dive deep in.....

Installing Python


  • To install Python in your system, visit this link
  • Select the correct version of Python compatible with your operating system and click on download button.
  • Install the downloaded file in your system and follow all the on-screen instructions.
  • Now, open the official Python IDE.

Installing Python in Android

Don't have a PC, but want to learn Python?

Don't worry we have a solution for you. First, install Termux in your android phone from this link. Then follow this link

Successfully installed Python? Now, let's get started...

Creating your first program in Python














  • Open the python terminal and then type, print("Hello World!").
  • You will get the following output on your screen: 
                         Hello World! (see in the image above)

If you are using Termux, follow the same instructions and you'll get the same output.

                                                                            
Share:

What is Cryptography? | Concept of Cryptanalysis | Career opportunities in Cryptology

What is Cryptography? | Concept of Cryptanalysis | Career opportunities in Cryptology


Cryptography is related with the way toward changing over customary plain content into muddled content and the other way around. It is a technique for putting away and transmitting information in a specific frame with the goal that those for whom it is proposed can read and process it. Cryptography shields information from burglary or modification as well as be utilized for client validation. 


Share:

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