Ready to Start Your Career?

Lesson 2 - Learning Python!

jumb01 's profile image

By: jumb01

June 8, 2017

:~$ whoamiAslam AdmaniThis is lesson 2 in my Python series. If you'd like to start from the beginning, access the previous lesson here.In this lesson, we will cover the following:
  1. Data type
  2. Print statement
  3. Doc-string and commenting
  4. Variables
# A pound sign or hash tag sign is used to create comments within python.  In this post, I will use them to represent comments and pointers.Data types
  • string#or str
    • The string is entered into quotation marks. It represents standard text. As you may have seen in the last post, with the ending print 'hello world'. In this case, hello world is printed in string format.
  • integer#or int
    • Integer or int for short represents whole numbers. With these, you can do basic math, add, subtract, multiply and much much more. E.g. print 1+12, this will return 13 to as the output.
  • boolean#or bool
    • Boolean is a data type representing either true or false. In Python True and False are keywords, keep this in mind for later!
  • float
    • floats are numbers with a decimal place.  You can do basic math between floats and ints.
#One thing to note is that: You can not add a string with integers or else it will raise an error. If you wish to place a certain integer within your line string, you can convert the integer or float into a string by simply typing in str(12). 12 is just a random number I picked, but you can put whatever you need to.Print statementThe print statement is used to return some sort of value to the output. In order to run a python script you can open up a terminal or command prompt, get into the same directory/folder path (on Linux you can enter: cd ~/Desktop/PythonCode ~ = home and then on Desktop is a folder called PythonCode, and on Windows you can enter: chdir C:UsersYourUserNameDesktopPythonCode) that your python file is stored in, and enter: python This command will run your script.The print statement will be used ever-so-often! So it's something to get used to.print 'I hope you're enjoying this series!'#Wait, why is there a random character?In Python an '' is a key character and is capable of multiple things. Remember how I mentioned in the last blog that you can print using double quotes and single quotes. Well at some point you may come across the issue of having to use either a single or double quote inside of your string. In order to do so, you will need the character. It's a special key which tells Python something's going on here, do not treat the following character as a normal string character. So, what else can this '' character do?
  • '' ' displays a single quote within a single quoted string
  • "" "  displays double quote within a double quoted string
  • 'n ' creates a new line
  • 't '  creates a horizontal tab (spaces)
  • 'v ' creates a vertical tab
Now it's your go, give it a shot, play around with these, the backslash characters, strings, integers, floats, see what you can do. Where you get errors, what errors are they? Why is it an error, how do we fix *Note: this is a big part of coding, to break it down and figure it out, attempt to resolve any issues etc.*At this point, without referring to any source, see if you could figure these out, using boolean (True or False)e.g. print 'Hello world" = False, it opens the string with a single quote and closes with a double thus the code is broken.
  1. print "Hello world"
  2. print 'I am codiak, hahaha or they say
  3. print "I hope you guys benefit from this, I enjoy making these for anyone that learns"
  4. print "They're watching me"
  5. pr1nt  "Lol - this is a clueeeeeee" + " ooooooo"
  6. print 'errrrmm what am I talking about? ' + "what is going on"
  7. print '  Hello World'
Done? Refer to: for answers!DocstringsWe touched upon docstrings in Lesson-1, however, within large amounts of code they're good practice and help with code maintenance. So, let's go over them again.Docstring uses a triple quote syntax. It requires either three single quotes or three double quotes, and must close in the same fashion. For example: ' ' 'Hello my name is Aslam Admani, and I enjoy writing this post for anyone that's new to programming and wants to learn Python programming! ' ' ' Docstring doesn't need to be printed, it could sit in your code to remind you of certain functions that may be in use, and why, to help when returning to the code.VARIABLESSo far we've only touched upon datatypes, the print statement, and functions. So let's dive into variables. You can think of them as containers, they're something you store an item in, and when you want to access, you can grab them out at your will.A variable could be named anything, much like a container could look like anything really. However, it's best practice to name the variable to something which is relevant to your code.The syntax of a variable is as follows:var = 'value'#var short for variable, = meaning it is, 'and in this case the value is a string'. However, the value could be one of any mentioned datatypes. So for instance if we're looking to store someone's name and ages in variables you would do:name = 'c0diak'age = 10000#we have stored and int in the variable (container) age #& Of course this is not my real age!Once we have stored these items in their variables we can now do things with them:print name, ageprint 'my name is ' + name + 'and I am' + age + 'years old'### Oops we get an error... So let's think logically and fix it with what we've learned in this post.1. The variable named 'age' stores an integer2. Our print statement contains string3. We know these don't go together4. We can convert the integer value to become a string. We do this by typing:  str(age)Remember how before I typed str(12), str(age) is the same thing, except of course the data is now stored in a variable and can be accessed whenever we want from our code. Since I've declared these two variables, I can access their data 600 lines later, or 1000 lines, or even 6000 lines later. However, there could be some limitations depending on where the variable has been declared.We will thoroughly go through variables in the next blog!Until then, I'd suggest you research variables, and play around with them. Use what you've learned thus far. ~ you can use this link to find the answers to earlier questions, and more challenges plus sample codes!If you've gotten to this point of the post/series, I thank you ever-so-much for tuning in! Hope to see you for the next lesson. Once having gone through the basics of Python programming, we could look at how to use Python for hacking and other interesting stuff!
Schedule Demo