Blog - Python Fundamentals


This blog will explain basic Python Fundamentals that are a precursor for more advanced Python programs. It will cover methods like print, type, len, input. This list is not exhaustive and there are many more that this blog might not cover. However, I hope this blog is simple and easy to understand, hopefully giving you a good starting point to the amazing world of python.


Print Statement


This is the probably the first thing you will learn when you learn python. See below.


print(“hello”)


This is a usage of the print statement. When this code is run, it will output hello. 


The structure of a print statement is print(“ type word in here “). REMEMBER to include quotation marks otherwise there will be an error.


Now it's your turn! Try more examples.


Print can also be used for basic math operations. See below.


print(1+2)


This will output 3. REMEMBER to not use quotation marks if you want to see the result of a math operation. Otherwise, it will output 1+2, but not 3.


Now it's your turn! Try more examples.




Correct Syntax


Correct syntax is also very important. Lets go back and revisit the print statement. When trying to print a word, both quotation marks have to be present. Brackets are also required(The one above the 9 and 0). Below are four wrong examples.


print(hello”)

print(“hello)

print[“hello”)

print hello”)


Below now is two correct examples


print(“hello”)

print(5+7)


Notice that for math operations, quotations are not needed. But if you did include quotations, the output would be different. See below


print(5+7) outputs 12

print(“5+7”) outputs 5+7


Order of operations is also important. There are +, -, *, / , **, % and //.


print(5+7*3) outputs 26

print(5*7+3) outputs 38


Guess what this outputs: print(99**(2 * 9)


Options: A. 99^18 B. 99 * 18 C. 99 + 18 D. error



The Answer is A. 



Variable Creation


Now lets look into how we can create variables.


First, we need to have a variable name. Now, here are four rules you have to follow when creating variable names.


  1. No uppercase

  2. Two words have to be separated by an underscore

  3. Variable names can’t start with a number

  4. Variable names cannot be a commands, like print()


After we write the name,  we need an equals(=) sign, then a value. The value can be anything, but the syntax has to be correct. If it is a number, write it directly. If it is a word, there has to be quotation marks. If it is a boolean operator(True, False), write at directly WITH the first letter capital.


Example:


a = 0

dog_234 = 0

i_love_meat = “meat”

a_4_5_6 = 15

existence = True


Now it’s your turn: Which of the following options will result in an error


62 = 62, a5 = 5, i_hate_meat = “hate”, dog = True


The correct answer was 62 = 62, variable names can’t start with a number.






Comments

Popular posts from this blog

Pytorch Basics