Python for absolute beginners

VARIABLES :

Variable act as a sort of placeholder for whatever you want to put in them and variables can be changed so hence the name variable.

The way to define a variable is very simple it’s just some text let’s say

m = 100 (or anything)

print (100)

They can have int, strings, function, data structure-like list , array all sort of things we can have operations also like –

OpVar = m/5.3

print (opVar)

Limitations of variable is We cannot start a variable with number like 1var =5 //this will show error

But if you want to put a number in starting…which I don’t know why you will need it so you can have that variable name with _1var = 5, print(_1var).

(To get ‘Hands-on python for absolute beginners’ course click on the link → https://bit.ly/2L4Tl9d)

Conditional Statement in Python

Conditional statement in python perform different computations or actions depending on whether a specific Boolean constraint evaluates to true or false.

While Loop :

There are basically two major loop in python programming language that is the While loop and For loop.

So , Let’s just first talk about the while loop first .

So the while loop does what is sounds like it basically question

“ while conditionis the such do something “ and it will continue running while block of code until the condition is no longer the case

Conditional Statement Example :

Let’s say condition = 1

while condition < 10 :

print (condition)

condition += 1 // this is condition = condition +1.

# — single like comment, ‘’’multiple line comment’’’

Condition = ‘5’

While condition is <15:

print (‘True’). //error string

(To get ‘Hands-on python for absolute beginners’ course click on the link → https://bit.ly/2L4Tl9d)

For Loop :

For loop is used for many of the same exact task as the while loop , generally you will see the while loop use for finite task , and for loop is use for the task that has variable time frame that are uncertain time frame . While loop and For Loop can be use interchangeable.

For Loop is often use with the list , or in others word it is use to iteration or to make a counter through the list .

Examples

  1. exampleList = [1,3,8,9,2]

for i in exampleList : // Here ‘i’ = Number in exampleList

print ( i )

2. for i in range (1, 11) : // counter //generator

print (i)

(To get ‘Hands-on python for absolute beginners’ course click on the link → https://bit.ly/2L4Tl9d)

If Statement :

If statement is pretty simple if statement is going to ask

“if something (case) is True do something.”

x = 2

y =5

z =10

if x> y:

print (x ‘is greater than’,y)

if x < y:

print (x ‘is less than’,y)

if x == y: // = — is as assignment operator , not a comparison operator

print (x ‘is same as ,y)

if x <= y:

print (x ‘is less than equal to’, y)

if x >= y:

print (x ‘is less than greater to’, y)

if z > y > x < z > y :

print (z,’is greater than ’ , y, ‘which is greater than ‘ , x )

(To get ‘Hands-on python for absolute beginners’ course click on the link → https://bit.ly/2L4Tl9d)

If else :

The concept of the else statement is to add some extra logic to your is statement , else can also be used in some other ways like with while loop , for loop and other but here is the idea is with if statement .

So here as I mentioned before here if statement is going to ask “ if something is the case do something “ and if it not the case do something else –

Examples

x= 3

y= 6

if x < y :

print ( x, ‘is less than’ , y)

else :

print ( x , ‘is not less than’ , y)

Example

x = 13

y = 6

if x < y :

print ( x, ‘is less than’ , y)

else :

print ( x , ‘is not less than’ , y)

Example

x = 13

y = 6

if x < y :

print ( x, ‘is less than’ , y)

if x > y :

else :

print ( x , ‘is not less than’ , y)

// does else will run , and the answer is no it will not run because the else statement is only going to pretend to the statement that immediately preceded it.

(To get ‘Hands-on python for absolute beginners’ course click on the link → https://bit.ly/2L4Tl9d)

If Elif Else

So the idea here is to add a another layer of logic to our previous If else statement and if you recall — if statement just run if the statement is true , the else statement will run if the preceding if statement is false and does not run but we saw that we could not do if if if and else and have all of the if’s or rather have the else to be contingent to all of the if’s.

So, this is something we want , so how to do it .

The way we could do that is by Elif statement , it is the short for Else if .

In Elif scenario the Elif will only run if the the previous if’s or previous elif’s do not run.

Examples

x = 3

y =6

z = 10

if x>y :

print(x ‘is greater then ’,y)

elif x < z :

print (x‘ is less than ‘, z)

else:

print (‘nothing was the case’)

Example

x = 3

y =6

z = 10

if x < y :

print(x ‘is greater then ’,y)

elif x < z :

print (x‘ is less than ‘, z)

else:

print (‘nothing was the case’)

Example

x = 3

y =6

z = 10

if x > y :

print(x ‘is greater then ’,y)

elif x > z :

print (x‘ is greater than ‘, z)

else:

print (‘nothing was the case’)

Example

x = 3

y =6

z = 10

if x > y :

print(x ‘is greater then ’,y)

elif x > z :

print (x‘ is greater than ‘, z)

elif y > z :

print (y, ‘is greater than’, z )

else:

print (‘nothing was the case’)

Example

x = 3

y =6

z = 10

if x > y :

print(x ‘is greater then ’,y)

elif x < z :

print (x‘ is greater than ‘, z)

elif y < z :

print (y, ‘is greater than’, z )

else:

print (‘nothing was the case’)

// now here we have two elif statement which is true so what will happen here ?

So again this will only run one statement it will check all statement and some statement is true then it will “break” ,

And what break does is that if we have some block of code and if you want to get out of that block of code then you break .

And break is literally a python command

What if you want to combine multiple if and elif and you want to be it contingent on the multiple if then this is also possible –

Example

x = 3

y =6

z = 10

if x > y or x < y : // think or & and command can be other if

print(‘something here was the case’)

elif x < z :

print (x‘ is greater than ‘, z)

elif y < z :

print (y, ‘is greater than’, z )

else:

print (‘nothing was the case’)

Example

x = 3

y =6

z = 10

if x > y and x < y : // think or & and command can be other if

print(‘something here was the case’)

elif x < z :

print (x‘ is greater than ‘, z)

elif y < z :

print (y, ‘is greater than’, z )

else:

print (‘nothing was the case’)

(To get ‘Hands-on python for absolute beginners’ course click on the link → https://bit.ly/2L4Tl9d)

Functions

  • A function is to assign a set of code and variable to that code know as parameter so that they can do a given specific job or Function is a group of code .
  • We make a function so that we can write a block of code which can perform any specific job which you want or written inside the function based on your logic and can call again and again whenever you want by the name (call by the name ) or by the reference (call by reference) inside your code .
  • You can write as much function as you want. It’s totally on you.
  • To make function use ‘def’ (keyword) before your function name .
  • You can give any name to your function but choose a unique name which does not conflict .
  • ( ) know as paranthas and this is where parameter of your function goes .It can be empty also.
  • Parameter — Parameter are value , datatype or anything which your function will use.

(To get ‘Hands-on python for absolute beginners’ course click on the link → https://bit.ly/2L4Tl9d)

Function Syntax

def example ( ) :

// def keyword to define a function, // ‘example’ is a function name

// your code goes here.

Example of code not using function :

x = 1

y = 3

print (x+y) //output will be 4

Example of Same Code using function

def example ( ) :

x = 1

y = 3

print (x+y) //output will be 4

Function can also have all the loop (while, for, if , if-else, if-elif) inside it , honestly, it can have all kinds of code .

Example :

def example ( ) :

x = 1

y = 3

print (x+y)

if x < y :

print (x, ‘is less than’ , y).

def main( ) :

example ( )

// calling function by name inside the main function and this will run the above code.

Function Parameters :

Basically what a parameter are ‘they are like extended variables’ and it allows programmer to customise the function itself.

For example if we are making simple calculator then :

(To get ‘Hands-on python for absolute beginners’ course click on the link → https://bit.ly/2L4Tl9d)

Example :

def addition (num1, num2) :

answer = num1+num2

return answer

// This function is return type function which means that it will return some value to main function.

// ‘ return ’ is a keyword which is used when the function return the value to the main function

x = addition (5, 6) // passing value to addition function and storing to ‘x’

print (x) //output will be 11

// There is no limit of parameter which you can have like taking above example

Example :

def addition (num1, num2, num3, num4) :

answer = num1+num2+num3+num4

return answer

x = addition (5, 6,5,6)

print (x) //output will be 22

But writing all the variable in the parameter has some issue and generally not recommended. It does not mean you can’t write but this is not good programming practice and the reason is –

Let’s say you have defined a function to write a website which have ’n’ number of variables like

def website (font, background_color, font_size, font_color) :

print(‘font’ , font)

print(‘bg’ , background_color)

print(‘font size’ , font_size)

print(‘font color’ , font_color)

(To get ‘Hands-on python for absolute beginners’ course click on the link → https://bit.ly/2L4Tl9d)

Let’s say you you give value to website function :

Case 1 :

website(‘TNR’ , ‘White’ , ‘11’ , ‘Black’)

Output :

font : TNR

bg : White

font_size : 11

font_color : Black

// which is right output.

But what if i put value in wrong place in website ( ) :

Case 2 :

website(‘11’ , ‘Black’ , ‘TNR’ , ‘White’)

Output :

font : 11

bg : Black

font_size : TNR

font_color : White

// This is not a right output which we need .

(To get ‘Hands-on python for absolute beginners’ course click on the link → https://bit.ly/2L4Tl9d)

Problem and solution while assigning / writing value many variables in function parameter:

  • You have assign values to function parameter (in our case website( ) ) in same order in which you have assigned the variable while defining the function (in our case — def website (font, background_color, font_size, font_color) )

Solution for this is :

def website (font = ‘TNR’ , background_color = ‘White’ , font_size = ‘11’ , font_color = ‘black’) : // This will be the ‘Default Value’ if there is no value given !

print(‘font’ , font)

print(‘bg’ , background_color)

print(‘font size’ , font_size)

print(‘font color’ , font_color)

website (background_color = ‘grey’)

// If value is given they will change the default value

(To get ‘Hands-on python for absolute beginners’ course click on the link → https://bit.ly/2L4Tl9d)

Global and Local Variables :

The terms ‘Global’ and ‘Local’ corresponds to the variable reach –

Global Variable :

Global Variable is the one which can access everywhere inside the program “Globally”. Global variable can be accessed locally but they can’t be modified locally inherently.

Ways to define Global Variable :

x = 6 // Variable outside the def example ( ) function “Global variable”

def example ( ) : //example is a function name

z = 5

print (z)

Output : 5

def example2 ( ) : //example2 is function name

z = 7 // Variable inside the def example ( ) function “Local variable”

print (z)

y = x + 1 // x is “Global Variable and can be access everywhere in program.

print (y)

return y

x = example2 ( )

Output : 7, 7, 7

def example3 ( ) : //example3 is a function name

global x

x+= 1

print (x)

Output : 7

(To get ‘Hands-on python for absolute beginners’ course click on the link → https://bit.ly/2L4Tl9d)

Local Variable

Local Variable is basically the opposite it can be only accessed “Locally” inside the give function or class it is defined. Local variable cannot be accessed globally inherited but it can modified.

Example Local Variable :

x = 6 // Variable outside the def example ( ) function “Global variable”

def example ( ) :

z = 5 // Variable inside the def example ( ) function “Local variable”

print (z) // output will be 5

But if you want to print ‘z’ outside the function example like let’s say you said you said print (z) outside the def example ( ) function , It will show error and you cannot do that –

Reason :

Variable z is defined “Locally “ inside the function def example ( ) and outside of the function def example ( ) does not exist.

(To get ‘Hands-on python for absolute beginners’ course click on the link → https://bit.ly/2L4Tl9d)

Working with Print Statement, Strings & Maths

We will talk about print function so basically a function has its name and we put parameter of the function in the brackets which is known as parentheses so a print function all it’s going to do is to output whatever text we put in or really whatever we put in as its parameter . This could be text,this could function , or parameter all sorts of things .

As far as string is concerned,First we have to have quote around our string in order to print it .

Example

  • print(‘Hello World !”)
  • print(‘’Hi!”)
  • print (‘concatena’ + ‘tion’)
  • print (‘hello’ , ‘there’)
  • print (‘ I am ‘ , 5 ). // this will not show error because we are not doing concatenation.
  • If you write print (‘I’ m 5) -> this will throw exceptional.
  • print ( ‘I\’ m 5) -> to print I’m 5
  • print (‘He said “Hi” ‘)

Both will work.

print ( 5 + ‘ I am ‘) -> but this will throw error because we are concatenation integer datatype to string datatype.

(To get ‘Hands-on python for absolute beginners’ course click on the link → https://bit.ly/2L4Tl9d)

Maths :

  • print ( 1 + 3 ) -> 4
  • print ( 1–3 ) -> -2
  • print ( 1 * 3 ) -> 3
  • print ( 1 / 3 ) -> 0.33333333
  • print ( 1.5 / 3.6 ) -> 0.4166666663
  • print ( 4 ** 2) -> 16 // 4 ** 2 is equal to 4 ^ 2 ( 4 square )

Errors :

Most of the time the error are result of typo,naming of variable or variable parameters and it is very common to misspell and overlook and this can throw the name error.

Example 1 : NameError

  • variable = 55

print(varaible)

Example 2 : Expected an indent

  • def func1 ( ) :
  • def func2( ):

print ( 2 )

//This will throw indentation error because python is excepting code after func1

Example 3 : Unexpected Indent :

As python is an scripting language and it runs line by line

A Syntax error will stop program to run entirely, while exception error is going to run the code all the way to that exception point.

def task( ) :

print(‘1’)

print (‘2’)

print(‘3’)

example 4 : EOL error

Print (‘hello world.

— — this will throw EOL error because you never close your print and string

Print (‘hello world ‘

— — this will throw Unexpected EOF error because sure you have closed your string but not print .

(To get ‘Hands-on python for absolute beginners’ course click on the link → https://bit.ly/2L4Tl9d)