Introduction to Python Basics
Hello everyone, welcome to our new series on Python which will prep you for Python Exploitation Development, Automation and Application Management! Today's Blog out of many to come will be the basics and nothing too hard, so if you're aware of this please skip this blog unless you want to refresh your memory.
What will be covered?
- The print() function, what they are and examples
- Variables, what they are and examples
- The input() function, what they are and examples
- Operators two types, what they are and examples
- Random parts like importing a module, pip etc
- A challenge for you to complete with the knowledge you gained.
Just basic knowledge
When we're using Python, we will use functions and these functions will always have parenthesise's "()"
When we're using strings they will always be wrapped in double quotes or single quotes for example using a string with a variable
s = 'hello' d = "hello"
Importing a module
import module_name import module_name or import module, module2, etc
Installing pip and using pip (Debian)
What is pip? Pip is a Python Package Manager that allows you to install packages, dependencies that didn't come with the original pre-filled Modules and Dependencies.
sudo apt-get install python3-pip
python3 -m pip install package_name python3 -m pip install SomePackage>=version # Specific Version python3 -m pip install "SomePackage>=version" # Minimum Version # Upgrading a package python3 -m pip install --upgrade SomePackage
print() function is one of the most commonly used functions in Python because it's role is to display text, responses etc on a terminal, for example, if I wanted to output "Hello I am a hacker" onto someone's terminal I could simply use the following syntax
print("Hello I am a hacker")
We can also work with multi-line which is a thing in Python that allows us to print onto a new line for example if I wanted to print the following message
print("Hello, I am John! I am learning Python")
This would cause an error due to the fact that there is no end double/single quote or parenthesise thus causing an error although we ended it on the next line the function does not know how to read that it only knows how to read one line strings so we use a "multi-line" example syntax
print("""Hello, I am John! I am learning Python""")
print() + Variables
What is a variable? A variable is nothing more and nothing less than something that holds a value such as a user input or a function, this input can be hardcoded too. Why would we need a variable you may ask? We use variables when we're wanting to keep stuff clean and short while re-using the value of that variable.
variable_name = "Hello" print(variable_name)
We can now continue writing our code and then later on instead of having to use
print("Hello") we can do a short variable name like
print(x) which will print the value of that variable in our case "Hello" however, you cannot use this variable in a
print() function the variable has been defined below the actual attempt at printing the variable name.
print(var) var = "Hello"
This will cause an error because the variable has not been defined before we try calling it.
input() function + print()
input() function is a function that allows us to take user input for example if we wanted to receive the users name we could ask them to enter a value. Let's take a look at an example:
input("What is your name?:")
Cool but you may be asking how can we use this? Well, variables can come in place to store the user input data and then we can relay that later for example:
name = input("What is your name?:") #ask the user for their name print(name) #print back the values they entered
input() function also has a method called
int() which only parses integer values (numbers not decimals) for example
inte = int(input("Enter an age:")) # integer input print(inte) #print the value
Operators (Two Types)
We can multiply, Add and Subtract numbers together with Python (As seen below we can do more) Why would we need this? For example, you've been asked to build a basic calculator you'd want to know this.
x = 10 f = 20 print(f + x)
You might have realised I haven't strung the values for the variable
x and f this is because if I do it'll be parsed as a string which would not add together but instead print out
1020 which isn't 10+20.
x = 20 f = 30 print(x * f)
An assignment operator is used when we want to pre-define an operator to a number, for example, if we have a variable with the value of 10 but want to multiply it by 20 we could use an assignment operator without using the
print() function to do so thus helps us work more productively.
x = 50 f = 30 f*= 30 # Multiples f by 30 print(x+f)
The original value of
30 however we multiplied the value by itself.
A Comparison Operator compares a value to another value for example if we wanted to check if a variable is equal or was more than the number stored in the variable we could do so.
x = 10 f = 30 print(x > f)
This defines two variables called
x which has the value of
f which has the value of
30 we then do
print(x > f) "x > f" is saying is x greater than f which returns false.
The challenge is to create a calculator that takes two numbers from a user, stores them and then multiplies the number then outputs the number.
If you struggle with this challenge read the two parts #1 variables #2 input() function + print() #3 Arithmetic Operators.
I hope you enjoyed this short blog. I will be retweeting everyone that tweets me a picture of their calculators! @RiotSecTeam
The next blog will be stuff like conditions, statements and building our first game then after that we will move on to Exploit Writing, Application Development and automation!