PYTHON : In 4 hours
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
LESSION ::: 1 :: PYTHON
PYTHON FOR ORACLE ::::
https://cx-oracle.readthedocs.io/en/latest/installation.html#quick-start-cx-oracle-installation
Link ::: https://www.youtube.com/watch?v=rfscVS0vtbw&t=387s
Installing Python ::: https://www.python.org/downloads/
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
You can use any of the test editors like notepad / tesxtedit to write your python code, however there are specifically designed text editors that are called IDE (Integrated Development Environment).
it is just a special environment where you can execute and run your Python code, since IDE are special test editors which helps to point out if there was an error in the code or syntax, therefore in this course we will using IDE's. And one of the most popular IDE for python is called PyCharm.
You can download PyCharm from -- https://www.jetbrains.com/pycharm/
So this is the IDE that we will be using for this course.
There are two versions of PyCharm, one is the professional version which you need to pay for it.
and the other version is the community version which is free and an open source
Community Version :::
This should have everything that you need to get started using Python.
After having Python and PyCharm now we are ready to start coding in python
Setup and Hello World ::::
So when you first open up Pycharm you are going to get a window as below
Create New Project
The configuration >> Preferences
Click New Project, you will be taken to the below screen where you can give your project name.
Named: Giraffe
Below that is the interpreter, what is out there is 3.6.3 , you can select the version of your interpreter
come down at the bottom right hand corner of the screen and click on Create.
You can now give a name to the file.
Now you are ready to start programming.
Drawing a Shape ::::
print (" / |")
print (" / |")
print (" / |")
print ("/___|")
The above program will print out a triangle.
Variables and Data Type ::::
Lets learn how to use variables in Python
When you are creating a variable we need to give a couple of information to python, what you need to give python is the name of the variable.
When we are defining a variable for Python we need to separate the words using an underscore
Variable name ::: >> character_name
character_name = "John" >> now we have a value for the variable name
In the above program, as you can notice the test before the variable "character_name" has been wrapped inside quotation marks followed by it is a plus sign + followed by the variable name "character_name" and again there is a quotation mark wrapped for comma
the above program instructs to type the text plus the variable value followed by rest of the test in the quotation
Lets understand the different type of values that you can store in these variables . In Python you have a datatype called
WORKING WITH STRINGS ::::
"Strings" >> which is all text
character_name = "Tom"
"Number" >> you can store a number in a variable which needs no quotation
ex : phone = 380090909
you can store a decimal number too
ex : number = 21.9090909 >> you can store any type of number
Boolean Values to a variable
Is_Male = True / False
Working with Strings ::::
String are basically plain text that we want to have inside of our program, and all the different things that you can do with strings.
Print ("Giraffe Academy")
print ("Giraffe\nAcademy") >> The \n new line character will place the second word in the next line
print ("Giraffe\n"Academy")
phrase = "Giraffe Academy"
print(phrase)
The below does Concatenation >> which means adding a string to another
phrase = "Giraffe Academy"
print (phrase + " is cool")
The above will print "Giraffe Academy is cool"
In addition to all that we can use Function to strings which can allow you do a verity of things.
when you say phrase. it pops up the below window and in our case we are choosing the lower case to print the phrase in "lower case"
the above function will convert the entire character to lower case. the below function converts the string to upper case.
in spite of converting the string to upper case or lower case I can entire check if the string is an upper case or a lower case
The above function checks if the string is entirely in upper case or in lower case. This will give out a false or a true value, This will return a value as false.
You can even combine the functions together,
The above program shall convert the string to upper first and then compare if the string is entirely in Upper case. you can also figure out the length of this string.
The LEN is a function which check the number of characters of the string.
We can also get individual characters inside of a string for example if I want to grab one of these characters, Say for example if you want to figure out what is the first characters in this string is.
Here you will open up and open and closed square brackets and in here you can specify the index of the number that the character that you want to grab.
you can give the index number as 0 which should give you the first character in the word.
In python when you work with strings, the strings get indexed starting with 0
There is also an awesome function that you can use which is called an Index function, the index function will tell us where exactly is the specific character or a string is exactly located inside of a string. The below index function will return the index of the character G
The above will return the index number if the character G in the string. which is 0
There is one more function that is called replace which will help you replace a character or a specific string.
The above program shall replace the string value of "Giraffe Academy" to "Elephant Academy"
WORKING WITH NUMBERS ::::
print (2) >> will print out 2
print (2.0897) >> shall print out 2,0897
You can even print a negative number
print (-2.0897) >> -2.0897
In Python you can use basic arithmetic's
print (3 + 4.5)
print (3 * 4.5)
print (3 / 4.5)
And if you want to make complex operations
print (3 * 4 + 5) >> it will first multiple 3 *4 and then plus it with 5 and the ans is 17
print (3 * (4 + 5)) >> this changes the order in this case it makes an addion 4+5 = 9 and 9 is multipled by 3 and we end up with 27
print ( 10 % 3) It is called 10 mod 3 >>> this takes the first number and divide it by the second number and it is going to spit out the reminder .
We can convert the number 5 to a string with the STR function
this will print the number 5 along with a string . There are various .
Function :::: abs ::: absolute value of the number
my_num = - 5
print (abs(my_num))
The answer is 5, because it is absolute value of -5
Function :::: pow --power
We can pass on two parameters to this number, first the number and the power to which it needs to be multiplied.
print (pow(3,2)) >> which is 3 raised to the power of 2
Function :::: max
print (max(4, 6) >> This will return the larger of the two numbers when you pass into it
Function :::: min
print (min(4, 6)
Function :::: round
print (round(3.2)) >> Gives out 3 as the round figure
There are many more functions in Python and in-order to use them, we need to import those function -- which means importing external code into our file, you want to access this specific math functions there is something called Python Math
If I want to import some of the math functions into our code
>>from math import * // This will go out and gather some math function that we can use.
Function :::: ceil >> round the number upwords
print (ceil(4.7)
Function :::: floor
print (floor(3.7)) >> This will chop of the decimal point and grab the small number
Function :::: sqrt
print (sqrt(36)) >> This will return the square root of the number 36
math >> is called a module ::: you don't have to worry too much what that is right now - just know that when we put this line of code into out program it gives us it gives us access to a lot more math functions.
GETTING INPUTS FROM THE USER :::::
input()
input ("Enter your name :")
name = input ("Enter your name :") >> the input received is placed into a variable "NAME"
print ("Hello" + name + "!")
BUILDING A BASIC CALCULATOR ::::
The above program collects two numbers and assigned it to the variables num1 and num2 and adds them and prints out the result.
Python when accepts an input it default assumption is that it is a string, in order to have the input accepted as number you may have to convert them to numbers. there are two python function that helps you to convert the strings to numbers one is INT as in the below line.
result = int(num1) + >> This will convert this to an integer number, an integer number is a whole number
result = int(num1) + int(num2) >> now both the number get converted to integers or as whole numbers that does not have decimals and it is going to add them up and prints out the result.
The above program has a drawback as it only works fine with whole numbers as the function used is INT.
There is another function that we can use, instead of INT we can use FLOAT and it works fine for both the decimal and whole numbers.
MAD LIBS GAME ::::
We are building a Mad Libs Game in python , a Mad Libs Game is where you can enter a list of random words.
The above program is the code for the mad libs game.
LISTS ::::
Storing the list of information, a loads of time when you are working with python you could be dealing with large amounts of data, when you are dealing with large amounts of data make sure you can manage it, organize it properly. A list is generally a structure that you can use inside of Python
to store lists of information's, so you can take a bunch of data values and you can put them inside a list and allows to organize them and keep track of them a lot easier, so generally you would create a Python list , put a bunch of related values inside of that and then you can use it for programs, lets us look at some of the basic use cases of using lists
You create a List a lot like you would create a Python variable. We will give a descriptive name basically describing what is there in the list . you will use open and close square brackets, so whenever you use python it automatically know that they want to store a bunch of values
friends = ["Kevin", "Kerin", "Tim"]
Now that we have created the list , how can we access a specific value inside that list
when you say print (friends), it prints down the below
["Kevin", "Kerin", "Tim"]
each one of these values have a specific index that stores the values
print (friends[0]) >> prints out Kevin
You can even display the values from back of the list
print (friends[-1]) >> which will display Tim
If I want to display index position 1 and all the values after that
print (friends[1:]) >> ["Kerin", "Tim"]
print (friends[0:2] >> it will print out ["Kevin", "Kerin" ] and skip the second one and the one that follows 2
If you want to change the value of the index position at 1 to something else
friends[1] = "Mike"
LIST FUNCTION ::::
The list is container which store a bundle of information and the list comes with a verity of functions
with it
Function ::: extend
friends.extend(lucky_number)
Result :::
The result has both the output :
['Tommy', 'Jennifer', 'Mike', 'Toober', 5, 6, 78, 46, 57, 78]
Function :::: append
The append function allows to add a element to the existing values
Result :::
['Tommy', 'Jennifer', 'Mike', 'Toober', 5, 6, 78, 46, 57, 78, 'Armor']
Function :::: insert
friends(1, "jelly") >> This inserts the value in the index 1 with the value "Jelly"
Function :::: remove
friends.remove("Toober")
Function :::: clear >> removes all the values in the list
Function :::: pop >> Remove the last value in the list
Result :::
the last figre 78 is removed.
['Tommy', 'Jennifer', 'Mike', 'Toober', 5, 6, 78, 46, 57]
Function ::: Check if a certain value was in the list
print(friends.index("Mike")) >> this will look for the value "mike" in the list and displays its index
Function :::: to check the count of the values in the list
print(friends.count("Jim")) >> displays the number of count that it appears.
Function :::: sort() >> This will sort the list in ascending order.
friends.sort() >> The list displays in ascending order
print(friends)
lucky_numbers.sort() >> The numbers is sorted in ascending order.
print(lucky_numbers)
Function :::: reverse >> This will reverse the list
lucky_numbers.reverse()
Function :::: copy() >> copy()
friends2 = friends.copy() >>
print(friends2)
----
TUPLES ::::
A Tuple is a type of data structure, which basically means it is a container, where we can store different values, now if you are familiar with lists in Python, A tuple is very similar to a list. It is basically a structure where you can store multiple pieces of information
A tuple has some key difference from a list
How to create Tuple :::: >> one of the most common of tuples is co-ordinance, lets say we have a series of x,y co-ordinance
coordinates (4, 5) >> Tuples are immutable, we hear that word a lot, basically means that that a Tuple cannot be changed or modify it
You can use any of the test editors like notepad / tesxtedit to write your python code, however there are specifically designed text editors that are called IDE (Integrated Development Environment).
it is just a special environment where you can execute and run your Python code, since IDE are special test editors which helps to point out if there was an error in the code or syntax, therefore in this course we will using IDE's. And one of the most popular IDE for python is called PyCharm.
You can download PyCharm from -- https://www.jetbrains.com/pycharm/
So this is the IDE that we will be using for this course.
There are two versions of PyCharm, one is the professional version which you need to pay for it.
and the other version is the community version which is free and an open source
Community Version :::
This should have everything that you need to get started using Python.
After having Python and PyCharm now we are ready to start coding in python
Setup and Hello World ::::
So when you first open up Pycharm you are going to get a window as below
Create New Project
The configuration >> Preferences
Click New Project, you will be taken to the below screen where you can give your project name.
Named: Giraffe
Below that is the interpreter, what is out there is 3.6.3 , you can select the version of your interpreter
come down at the bottom right hand corner of the screen and click on Create.
You can now give a name to the file.
Now you are ready to start programming.
Drawing a Shape ::::
print (" / |")
print (" / |")
print (" / |")
print ("/___|")
The above program will print out a triangle.
Variables and Data Type ::::
Lets learn how to use variables in Python
When you are creating a variable we need to give a couple of information to python, what you need to give python is the name of the variable.
When we are defining a variable for Python we need to separate the words using an underscore
Variable name ::: >> character_name
character_name = "John" >> now we have a value for the variable name
In the above program, as you can notice the test before the variable "character_name" has been wrapped inside quotation marks followed by it is a plus sign + followed by the variable name "character_name" and again there is a quotation mark wrapped for comma
the above program instructs to type the text plus the variable value followed by rest of the test in the quotation
Lets understand the different type of values that you can store in these variables . In Python you have a datatype called
WORKING WITH STRINGS ::::
"Strings" >> which is all text
character_name = "Tom"
"Number" >> you can store a number in a variable which needs no quotation
ex : phone = 380090909
you can store a decimal number too
ex : number = 21.9090909 >> you can store any type of number
Boolean Values to a variable
Is_Male = True / False
Working with Strings ::::
String are basically plain text that we want to have inside of our program, and all the different things that you can do with strings.
Print ("Giraffe Academy")
print ("Giraffe\nAcademy") >> The \n new line character will place the second word in the next line
print ("Giraffe\n"Academy")
phrase = "Giraffe Academy"
print(phrase)
The below does Concatenation >> which means adding a string to another
phrase = "Giraffe Academy"
print (phrase + " is cool")
The above will print "Giraffe Academy is cool"
In addition to all that we can use Function to strings which can allow you do a verity of things.
when you say phrase. it pops up the below window and in our case we are choosing the lower case to print the phrase in "lower case"
the above function will convert the entire character to lower case. the below function converts the string to upper case.
in spite of converting the string to upper case or lower case I can entire check if the string is an upper case or a lower case
The above function checks if the string is entirely in upper case or in lower case. This will give out a false or a true value, This will return a value as false.
You can even combine the functions together,
The above program shall convert the string to upper first and then compare if the string is entirely in Upper case. you can also figure out the length of this string.
The LEN is a function which check the number of characters of the string.
We can also get individual characters inside of a string for example if I want to grab one of these characters, Say for example if you want to figure out what is the first characters in this string is.
Here you will open up and open and closed square brackets and in here you can specify the index of the number that the character that you want to grab.
you can give the index number as 0 which should give you the first character in the word.
In python when you work with strings, the strings get indexed starting with 0
There is also an awesome function that you can use which is called an Index function, the index function will tell us where exactly is the specific character or a string is exactly located inside of a string. The below index function will return the index of the character G
The above will return the index number if the character G in the string. which is 0
There is one more function that is called replace which will help you replace a character or a specific string.
The above program shall replace the string value of "Giraffe Academy" to "Elephant Academy"
WORKING WITH NUMBERS ::::
print (2) >> will print out 2
print (2.0897) >> shall print out 2,0897
You can even print a negative number
print (-2.0897) >> -2.0897
In Python you can use basic arithmetic's
print (3 + 4.5)
print (3 * 4.5)
print (3 / 4.5)
And if you want to make complex operations
print (3 * 4 + 5) >> it will first multiple 3 *4 and then plus it with 5 and the ans is 17
print (3 * (4 + 5)) >> this changes the order in this case it makes an addion 4+5 = 9 and 9 is multipled by 3 and we end up with 27
print ( 10 % 3) It is called 10 mod 3 >>> this takes the first number and divide it by the second number and it is going to spit out the reminder .
We can convert the number 5 to a string with the STR function
this will print the number 5 along with a string . There are various .
Function :::: abs ::: absolute value of the number
my_num = - 5
print (abs(my_num))
The answer is 5, because it is absolute value of -5
Function :::: pow --power
We can pass on two parameters to this number, first the number and the power to which it needs to be multiplied.
print (pow(3,2)) >> which is 3 raised to the power of 2
Function :::: max
print (max(4, 6) >> This will return the larger of the two numbers when you pass into it
Function :::: min
print (min(4, 6)
Function :::: round
print (round(3.2)) >> Gives out 3 as the round figure
There are many more functions in Python and in-order to use them, we need to import those function -- which means importing external code into our file, you want to access this specific math functions there is something called Python Math
If I want to import some of the math functions into our code
>>from math import * // This will go out and gather some math function that we can use.
Function :::: ceil >> round the number upwords
print (ceil(4.7)
Function :::: floor
print (floor(3.7)) >> This will chop of the decimal point and grab the small number
Function :::: sqrt
print (sqrt(36)) >> This will return the square root of the number 36
math >> is called a module ::: you don't have to worry too much what that is right now - just know that when we put this line of code into out program it gives us it gives us access to a lot more math functions.
GETTING INPUTS FROM THE USER :::::
input()
input ("Enter your name :")
name = input ("Enter your name :") >> the input received is placed into a variable "NAME"
print ("Hello" + name + "!")
BUILDING A BASIC CALCULATOR ::::
The above program collects two numbers and assigned it to the variables num1 and num2 and adds them and prints out the result.
Python when accepts an input it default assumption is that it is a string, in order to have the input accepted as number you may have to convert them to numbers. there are two python function that helps you to convert the strings to numbers one is INT as in the below line.
result = int(num1) + >> This will convert this to an integer number, an integer number is a whole number
result = int(num1) + int(num2) >> now both the number get converted to integers or as whole numbers that does not have decimals and it is going to add them up and prints out the result.
The above program has a drawback as it only works fine with whole numbers as the function used is INT.
There is another function that we can use, instead of INT we can use FLOAT and it works fine for both the decimal and whole numbers.
MAD LIBS GAME ::::
We are building a Mad Libs Game in python , a Mad Libs Game is where you can enter a list of random words.
The above program is the code for the mad libs game.
LISTS ::::
Storing the list of information, a loads of time when you are working with python you could be dealing with large amounts of data, when you are dealing with large amounts of data make sure you can manage it, organize it properly. A list is generally a structure that you can use inside of Python
to store lists of information's, so you can take a bunch of data values and you can put them inside a list and allows to organize them and keep track of them a lot easier, so generally you would create a Python list , put a bunch of related values inside of that and then you can use it for programs, lets us look at some of the basic use cases of using lists
You create a List a lot like you would create a Python variable. We will give a descriptive name basically describing what is there in the list . you will use open and close square brackets, so whenever you use python it automatically know that they want to store a bunch of values
friends = ["Kevin", "Kerin", "Tim"]
Now that we have created the list , how can we access a specific value inside that list
when you say print (friends), it prints down the below
["Kevin", "Kerin", "Tim"]
each one of these values have a specific index that stores the values
print (friends[0]) >> prints out Kevin
You can even display the values from back of the list
print (friends[-1]) >> which will display Tim
If I want to display index position 1 and all the values after that
print (friends[1:]) >> ["Kerin", "Tim"]
print (friends[0:2] >> it will print out ["Kevin", "Kerin" ] and skip the second one and the one that follows 2
If you want to change the value of the index position at 1 to something else
friends[1] = "Mike"
LIST FUNCTION ::::
The list is container which store a bundle of information and the list comes with a verity of functions
with it
Function ::: extend
friends.extend(lucky_number)
friends = ["Tommy","Jennifer","Mike","Toober"] lucky_number = [5,6,78,46,57,78] friends.extend(lucky_number) print(friends)
Result :::
The result has both the output :
['Tommy', 'Jennifer', 'Mike', 'Toober', 5, 6, 78, 46, 57, 78]
Function :::: append
The append function allows to add a element to the existing values
friends = ["Tommy","Jennifer","Mike","Toober"] lucky_number = [5,6,78,46,57,78] friends.extend(lucky_number) friends.append("Armor") print(friends)
Result :::
['Tommy', 'Jennifer', 'Mike', 'Toober', 5, 6, 78, 46, 57, 78, 'Armor']
Function :::: insert
friends(1, "jelly") >> This inserts the value in the index 1 with the value "Jelly"
friends = ["Tommy","Jennifer","Mike","Toober"] lucky_number = [5,6,78,46,57,78] friends.extend(lucky_number) friends.append("Armor") friends.insert(1,"jelly") print(friends)
Function :::: remove
friends.remove("Toober")
friends = ["Tommy","Jennifer","Mike","Toober"] lucky_number = [5,6,78,46,57,78] friends.extend(lucky_number) friends.append("Armor") friends.insert(1,"jelly") print(friends) friends.remove("Mike") print(friends)
Function :::: clear >> removes all the values in the list
friends = ["Tommy","Jennifer","Mike","Toober"] lucky_number = [5,6,78,46,57,78] friends.extend(lucky_number) friends.append("Armor") friends.insert(1,"jelly") print(friends) friends.remove("Mike") friends.clear() print(friends)
Function :::: pop >> Remove the last value in the list
friends = ["Tommy","Jennifer","Mike","Toober"] lucky_number = [5,6,78,46,57,78] friends.extend(lucky_number) friends.pop() print(friends)
Result :::
the last figre 78 is removed.
['Tommy', 'Jennifer', 'Mike', 'Toober', 5, 6, 78, 46, 57]
Function ::: Check if a certain value was in the list
print(friends.index("Mike")) >> this will look for the value "mike" in the list and displays its index
Function :::: to check the count of the values in the list
print(friends.count("Jim")) >> displays the number of count that it appears.
Function :::: sort() >> This will sort the list in ascending order.
friends.sort() >> The list displays in ascending order
print(friends)
lucky_numbers.sort() >> The numbers is sorted in ascending order.
print(lucky_numbers)
Function :::: reverse >> This will reverse the list
lucky_numbers.reverse()
Function :::: copy() >> copy()
friends2 = friends.copy() >>
print(friends2)
----
TUPLES ::::
A Tuple is a type of data structure, which basically means it is a container, where we can store different values, now if you are familiar with lists in Python, A tuple is very similar to a list. It is basically a structure where you can store multiple pieces of information
A tuple has some key difference from a list
How to create Tuple :::: >> one of the most common of tuples is co-ordinance, lets say we have a series of x,y co-ordinance
coordinates (4, 5) >> Tuples are immutable, we hear that word a lot, basically means that that a Tuple cannot be changed or modify it
print(coordinates[0]) >> show the index position of the
Difference between Tuples and Lists ::::
We can create a lust of Tuples
coordinates [(2, 3), (6, 7), (8,10)]
FUNCTIONS ::::
A function is basically a collection of code which performance a specific task, so I can take a bunch of piece of code which are basically doing one thing, I can put them inside of a function and then when I want to do that task or do that one thing that the function was doing, I can just call the function.
The above is a simple function code, function are all named in lower case
We can actually make these functions a little more powerful, what we can do is we can give them information, a loads of time when we write a function we would want to pass them information and these are called parameters. A parameter is a piece of information we give to a function
The above code has a function that accepts a parameter
You can even have more than one parameters, technically speaking you can have hundreds of thousands or millions of parameters inside the function.
Above code passes two piece of information name and age. and the age is passed as a strings with quotation marks.
You can pass on whole numbers as age but you may have to convert them to string in the function as below.
RETURN STATEMENTS ::::
Some time when you you call a function you want the function to return you some information , when you call a function sometimes you want it to return some information to you, such as executing the code and let you know how the execution went.
return ::: >> the return key word can allow python to return some function.
the below code returns the result of the cube.
The below code will return the value and store the result of the cube....
Anything you code below the return keyword will not get executed. whenever Python finds the return keyword it will break the function and get out of the function
IF STATEMENT ::::
If statements are special statements in Python that allows you to make decisions.
We are going to test it out using a Boolean variable. with an if statement
If the condition remains true, it will print out a first set of the code.
since the condition is false, this will not print the statement.
The condition has else to it to print out if the condition is not true.
And condition
Adding more conditions to the same program
IF STATEMENTS AND COMPARISONS ::::
The max number:: returns the max number
Wrote a Python script on unix
#!/usr/bin/python
# Hello world python program
print ("Welcome to python")
def max_num(num1, num2, num3):
if num1 >= num2 and num1 >= num3:
return num1
elif num2 >= 1 and num2 >= 3:
return num2
else:
return num3
print(max_num(3,2,1))
----
BUILD A BETTER CALCULATOR ::::
Here is what we can do, whatever the user inputs we need to convert it into a number, by default whatever the user input is taken as a string by default, but we need to convert these to a number for we need to multiple it subtract it , minus it etc.
The above if statement finds out which type of operator has been entered by the user....
if the operator is not any of the ones specified
DICTIONARIES :::::
In this definition the word would be the key, similar to your usual dictionary.
The below program to convert a three digit month name to its formal name.
Creating Dictionary in python...
All of the key words : such as jan , feb must be unique or it will throw an error.
There are ways how I can access the values associated to the keys.
This will return the value associated with "November"
get function:::
The benefit of using the get function here is that when we pass on a key which is not valid or a key which has no value associated to it it returns the result as "NONE".
Below is the program that I created.
The keys need not be strings they can be numbers too.
WHILE LOOPS ::::
A while loop is a structure of a code which enables you to execute a code multiple number of times.
BEGINNING A GUESSING GAME ::::
In this guessing game we are going to use all sorts of programming structure learned up to this point of course ,
Inside of the program we will have a secret word and the user will have to keep typing the word untill he gets the secret word.
Small code:::
lets assume I want to set a limit for user to guess the word, and if they don't guess the word in three time they are going to loose the game and if they guess the word inside of the three times they win the game.
The below program was successful.
The below one works fine as well.
>>> not(out_of_guesses) == > indicates a boolean value "False"
FOR LOOP ::::
This loop is a special kind of a loop, a lot of times we use for loop to loop through different arrays or you can loop through the different letters, string, or we can loop through a series of numbers, for loops provide a specific purpose
for w >> for loop variable represents a different value every time we go through the for loop
The above code prints out each letter in vertical order :
That's how we can use for loop, we can define a variable and in each iteration the loop will change
instead of using this loop with strings we can also use it with arrays.
the above program prints out friends from friends ...
in the above code you may give any random name to the variable in for loop >> friend
In the below code it will print a range if index numbers
it will print 0-9 index numbers >> that is 10 numbers
you can also specify a range of numbers
The above will print out a range of numbers from 3 to 9 excluding 10.
Say for instance if you want to find out how many elements were inside of the array.
you can find out >> len(friends)
This will give out the value 3 in the array...as they are 3 elements in the array or list.
The below query will allow to access each individual friend from the list
The above code will print out each value in the array with its value.
if you want to play around with the range of index by putting in some complex code
The above gives the complex ...
Result :::
Gooph Kuttu Mookha
['Gooph', 'Kuttu', 'Mookha']
1
Gooph
3
['Mookha', 'Kuttu', 'Gooph']
2D LIST AND NESTED LOOPS ::::
Creating two dimensional list :::
The above is a 2d list which can have a grid of lists. lets see how we can access individual elements inside this list structures.
the above code displays 2 row 2nd cloumn
NESTED FOR LOOP ::::
nested for loop is a situation when we have a for loop inside a for loop ...we can use a nexted for loop to print out all the numbers in a grid of from the 2 dimensional list.
The above code prints all the numbers in the above 2 dimensional list.
Result
1
2
3
3
4
5
4
5
6
3
++++Experimental code :::
Result
[1, 2, 3]
[3, 4, 5]
[4, 5, 6]
[3]
The below code is not working :::: we need investigate.
COMMENTS ::::
comment is a basically a special code in python which is not going to be rendered by python. Therefore, the python will ignore it and not going to execute it, comments are basically used by humans
You can use a hashtag symbol : #
Commenting is also helpful while you want to comment out a code
TRY EXCEPT ::::
Catching errors in python , sometimes most of the errors pop up and completely stop your program from running, we can indeed handle those errors and do things when the occur.
the above enables the program to respond accordingly rather than breaking the program to make your code more reliable and stable,
You want to try something like the try except block:
the except is capable of capturing the error and respond accordingly to display the
Try except :::
The above we need to check it out again
READING FILES ::::
In this chapter we are going to find out how to read from external files from python. In most scenarios you may want to read files from external sources such as text file or an html file. And you can actually use something called a Python read command. Which will allow you to read a file out side of your python files. You can use these files to get information .
open("employee.txt", "r") r for read mode
open("employee.txt", "w") w for write mode
open("employee.txt", "a") a stands for append
open("employee.txt", "r+") r+stands for read and write
r > read mode
we will store this open file to a variable
employee_file = open("employee.txt" , "r")
***Ensure one thing whenever you open a file you close that file as well.
employee_file.close() ::: This is going to close the file and now one is going to access it.
make sure that the file is readable,
print(employee_file.readable()) >> it is going to return a boolean value and is going to tell us whether or not
Now to read the file.
print(employee.read())
open("employee.txt", "a") a stands for append
Experimental Code ++++ ::::
>>> file_name = open("sree.txt", "r")
>>> print(file_name.read())
I am - here
You are there
print(employee.readline()) >> This readline function just reads the first line of the file.
print(employee.readlines()) readlines >> Will put them all in an array and all the information can be found in the array.
The above code readlines()[1] will read out the line which is there in index 1
The above code will create a for loop
WRITING TO FILES :::
Experimental Code ++++
>>> file_name = open("taste.txt" , "a")
>>> file_name.write(" This is a new lines")
>>> file_name = open("taste.txt" , "r") >> putting back into readable format
>>> print(file_name.read())
We need to very careful while appending or editing a file as we can have it messed up easily
.
The above code will append in the next line of the file.
Overwriting a file :::
>>> file_name = open("taste3333.txt" , "w") The word w will overwrite a file. This command creates a new file.
Creating a new file :::
The above code will create an html file with the paragraph below that you code in html.
MODULES AND PIP ::::
A Module is a python file that you can import into your current python file, for instance if I have already written a python code with multiple useful function and variables extra we can indeed import that code into a new code and use the functions in the new code.
There is a python code that the programmer has written which he wants to import into his current python file that he is working on.
This is the way to import the other code into your current program where in you should be able to use all those functions and program
Now python is smart enough to understand that it has to fetch all the stuff from the file useful_tools
When you type in print(useful_tools. >>You will fins all the functions that are listed in the file useful tools here. Therefore you can run that function.
Where to get huge list of modules that you can import into your program ::::
type in "list of python modules" in google
https://docs.python.org/3/py-modindex.html
if you are looking for a python code, perhaps you can look for "Python module to do" perhaps someone has already written a code of what you are looking for.
Where are these modules stored that we see on the python website ::::
>>> There are these build in modules in python so that you directly have accesses to them
>>> And there are extenal modules are stored in the same folder where we install python
External Libraries can be found in the left side of the screen in PyCharm
There is a folder up there called "Lib" >> THIS IS THE ONE WHICH STORES EXTENAL MODULES. The build in modules are not stored here.
When you click on any of the modules in the python website you can find where is this module located in python :::
Source code: Lib/argparse.py
There are tons of modules, and it can take you years to peep into each one of them if you were to learn what each of these modules do.
A lot of times you want to use Modules that other people have written, there are loads of developer who work on python and have written a loads of modules.
You can install those third party modules that does not come with python, lets see how to install such external modules.
Google :::::::Key word :: useful thirdparty python module
Python-docx ::: Can be installed using PIP which is a package manager (package manager enables you to install , manage , update and uninstall) .
If you have a newer version of python the PIP should come pre-installed. And if you do not have a newer version of Python you may have to install PIP separately.
check if the PIP is already installed in python
pip --version
CLASSES AND OBJECTS ::::
Classes and Object in python shall help you to make your program more organized and powerful.
in python we are dealing with all types of data.
Nor all data can be represented using , string, boolean and int, there are loads of things in the real word that we cannot represent as a string or a boolean etc for example, things like a Phone , a person or a computer cannot be represented in the datatype that the programming language. In other words the data types that we have in python can't cover that
With classed and Objects we can create our own data type :::: So I can create my own data type for anything I want in python.
for instance : I can create like a "Phone" datatype and I can represent a phone. So that i can store all the information that I ever wanted to store about my phone inside of that data type and in Python I can create a class for that, with class you can essentially create your own data type and it is super awesome and classes are extremely helpful.
Classes are used in every single major programming language that use there.
Basic Introduction of Classes :::
Lets say I am writing a program for a college or a university and I want to model a student. I want to model a real world object and it is a student. I don't have a student datatype , string or a number
for instance I can create a class called Student, Basically creating like a student data type.
Create a New file names student.py
Class Student :
{ Anything that is intended here becomes the attributes of the class student.
We will create an initialize function
what we are going to have inside of this function is we can map out what attributes a student should have.
The above will tell you the student name, major, grade_point_average, is on probotion
this is defining what a student is in our program
The below will not make any sense now but in a while it will tell you what it is all about.
The above is a template of what a student is::::
Now we come to the app.py file leaving the student.py file .
Now we can actually create a student and give it some information that is called an Object
A class is defining what a student is , A Object is the actual student.
In order to use that student class and create a student object we actually need to import that
from the student.py file import the student class
The above will print out the name of the student1 object
BUILDING A MULTIPLE CHOICE QUIZ ::::
In this session we will have the user take the quiz and we shall keep track his points and finally tell him what his score is.
This little array is called Question Prompts
We have to keep track of the actual Question itself and the Answer to it, so what we are going to do is create a question class. we are going to create a little data type for questions
==
===complete program
OBJECT FUNCTION ::::
We can create a lust of Tuples
coordinates [(2, 3), (6, 7), (8,10)]
FUNCTIONS ::::
A function is basically a collection of code which performance a specific task, so I can take a bunch of piece of code which are basically doing one thing, I can put them inside of a function and then when I want to do that task or do that one thing that the function was doing, I can just call the function.
The above is a simple function code, function are all named in lower case
We can actually make these functions a little more powerful, what we can do is we can give them information, a loads of time when we write a function we would want to pass them information and these are called parameters. A parameter is a piece of information we give to a function
The above code has a function that accepts a parameter
You can even have more than one parameters, technically speaking you can have hundreds of thousands or millions of parameters inside the function.
Above code passes two piece of information name and age. and the age is passed as a strings with quotation marks.
You can pass on whole numbers as age but you may have to convert them to string in the function as below.
RETURN STATEMENTS ::::
Some time when you you call a function you want the function to return you some information , when you call a function sometimes you want it to return some information to you, such as executing the code and let you know how the execution went.
return ::: >> the return key word can allow python to return some function.
the below code returns the result of the cube.
The below code will return the value and store the result of the cube....
Anything you code below the return keyword will not get executed. whenever Python finds the return keyword it will break the function and get out of the function
IF STATEMENT ::::
If statements are special statements in Python that allows you to make decisions.
We are going to test it out using a Boolean variable. with an if statement
If the condition remains true, it will print out a first set of the code.
since the condition is false, this will not print the statement.
The condition has else to it to print out if the condition is not true.
And condition
Adding more conditions to the same program
IF STATEMENTS AND COMPARISONS ::::
The max number:: returns the max number
Wrote a Python script on unix
#!/usr/bin/python
# Hello world python program
print ("Welcome to python")
def max_num(num1, num2, num3):
if num1 >= num2 and num1 >= num3:
return num1
elif num2 >= 1 and num2 >= 3:
return num2
else:
return num3
print(max_num(3,2,1))
----
BUILD A BETTER CALCULATOR ::::
Here is what we can do, whatever the user inputs we need to convert it into a number, by default whatever the user input is taken as a string by default, but we need to convert these to a number for we need to multiple it subtract it , minus it etc.
The above if statement finds out which type of operator has been entered by the user....
if the operator is not any of the ones specified
DICTIONARIES :::::
In this definition the word would be the key, similar to your usual dictionary.
The below program to convert a three digit month name to its formal name.
Creating Dictionary in python...
All of the key words : such as jan , feb must be unique or it will throw an error.
There are ways how I can access the values associated to the keys.
This will return the value associated with "November"
get function:::
The benefit of using the get function here is that when we pass on a key which is not valid or a key which has no value associated to it it returns the result as "NONE".
Below is the program that I created.
monthnames = {
"jan" : "january", "feb" : "Feburary",}
print(monthnames["jan"])
print(monthnames["feb"]The keys need not be strings they can be numbers too.
WHILE LOOPS ::::
A while loop is a structure of a code which enables you to execute a code multiple number of times.
i = 1while i <= 10: print(i) i = i + 1 print (" The loop has completed"
BEGINNING A GUESSING GAME ::::
In this guessing game we are going to use all sorts of programming structure learned up to this point of course ,
Inside of the program we will have a secret word and the user will have to keep typing the word untill he gets the secret word.
Small code:::
secret_word = ("Giraffe") guess = "" while guess != secret_word: guess = input ("Enter Guess : ") print ("You Win!"
lets assume I want to set a limit for user to guess the word, and if they don't guess the word in three time they are going to loose the game and if they guess the word inside of the three times they win the game.
The below program was successful.
secret_word = "Kuttu"guess = ""guess_count = 0guess_limit = 3out_of_guesses = False while guess != secret_word and not(out_of_guesses): if guess_count < guess_limit: guess = input("Enter Guess : ") guess_count += 1 else: out_of_guesses = True if out_of_guesses: print("Bro you loose!") else: print("Bro you Win !")
The below one works fine as well.
secret_word = ("Kuttu") guess = ""guess_count = 0guess_limit = 3out_of_guesses = False while guess != secret_word and not(out_of_guesses): if guess_count < guess_limit: guess = input("Enter Guess : ") guess_count = guess_count + 1 else: out_of_guesses = True if out_of_guesses: print("Bro you loose!") else: print("Bro you Win !")
>>> not(out_of_guesses) == > indicates a boolean value "False"
FOR LOOP ::::
This loop is a special kind of a loop, a lot of times we use for loop to loop through different arrays or you can loop through the different letters, string, or we can loop through a series of numbers, for loops provide a specific purpose
for w >> for loop variable represents a different value every time we go through the for loop
The above code prints out each letter in vertical order :
That's how we can use for loop, we can define a variable and in each iteration the loop will change
instead of using this loop with strings we can also use it with arrays.
the above program prints out friends from friends ...
in the above code you may give any random name to the variable in for loop >> friend
In the below code it will print a range if index numbers
it will print 0-9 index numbers >> that is 10 numbers
you can also specify a range of numbers
The above will print out a range of numbers from 3 to 9 excluding 10.
Say for instance if you want to find out how many elements were inside of the array.
you can find out >> len(friends)
This will give out the value 3 in the array...as they are 3 elements in the array or list.
The below query will allow to access each individual friend from the list
The above code will print out each value in the array with its value.
if you want to play around with the range of index by putting in some complex code
The above gives the complex ...
Tested out the code by modifying the code so it displays the index number and the condition.
EXPONENT FUNCTION ::::
exponent
function allows u to take a specific number and raise it to a specific
power. in python there is a really easy way to do this,
print(2**3) >> this simply means 2^3
We are going to see how a for loop can be used to create a function like this. so we are actually creating an exponent function
The result variable is just passed with the value 1,
And
inside the for loop the result is mulipled first time with the value 1
which will give the same result which ever is passed as the base_num ,
and the for loop checks the pow_num and again it multiplies with the
base_num and the result is stored into result and we are asking the
python code to return the value of result which is now 9 .
++++Experimental Coding ::::
friends = ["Gooph", "Kuttu", "Mookha"] print(friends[0], friends[1], friends[2]) print(friends[0:]) print(friends.index("Kuttu")) print(friends[-3]) print(len(friends)) friends.reverse() print(friends)
Result :::
Gooph Kuttu Mookha
['Gooph', 'Kuttu', 'Mookha']
1
Gooph
3
['Mookha', 'Kuttu', 'Gooph']
2D LIST AND NESTED LOOPS ::::
Creating two dimensional list :::
The above is a 2d list which can have a grid of lists. lets see how we can access individual elements inside this list structures.
number_grid = [ [1, 2, 3], [3, 4, 5], [4, 5, 6], [3],] print(number_grid[1][1])
the above code displays 2 row 2nd cloumn
NESTED FOR LOOP ::::
nested for loop is a situation when we have a for loop inside a for loop ...we can use a nexted for loop to print out all the numbers in a grid of from the 2 dimensional list.
number_grid = [ [1, 2, 3], [3, 4, 5], [4, 5, 6], [3],] for row in number_grid: for col in row: print(col)
The above code prints all the numbers in the above 2 dimensional list.
Result
1
2
3
3
4
5
4
5
6
3
++++Experimental code :::
number_grid = [ [1, 2, 3], [3, 4, 5], [4, 5, 6], [3],] for row in number_grid: print(row)
Result
[1, 2, 3]
[3, 4, 5]
[4, 5, 6]
[3]
The below code is not working :::: we need investigate.
COMMENTS ::::
comment is a basically a special code in python which is not going to be rendered by python. Therefore, the python will ignore it and not going to execute it, comments are basically used by humans
You can use a hashtag symbol : #
Commenting is also helpful while you want to comment out a code
TRY EXCEPT ::::
Catching errors in python , sometimes most of the errors pop up and completely stop your program from running, we can indeed handle those errors and do things when the occur.
the above enables the program to respond accordingly rather than breaking the program to make your code more reliable and stable,
You want to try something like the try except block:
the except is capable of capturing the error and respond accordingly to display the
Try except :::
The above we need to check it out again
READING FILES ::::
In this chapter we are going to find out how to read from external files from python. In most scenarios you may want to read files from external sources such as text file or an html file. And you can actually use something called a Python read command. Which will allow you to read a file out side of your python files. You can use these files to get information .
open("employee.txt", "r") r for read mode
open("employee.txt", "w") w for write mode
open("employee.txt", "a") a stands for append
open("employee.txt", "r+") r+stands for read and write
r > read mode
we will store this open file to a variable
employee_file = open("employee.txt" , "r")
***Ensure one thing whenever you open a file you close that file as well.
employee_file.close() ::: This is going to close the file and now one is going to access it.
make sure that the file is readable,
print(employee_file.readable()) >> it is going to return a boolean value and is going to tell us whether or not
Now to read the file.
print(employee.read())
open("employee.txt", "a") a stands for append
Experimental Code ++++ ::::
>>> file_name = open("sree.txt", "r")
>>> print(file_name.read())
I am - here
You are there
print(employee.readlines()) readlines >> Will put them all in an array and all the information can be found in the array.
The above code readlines()[1] will read out the line which is there in index 1
The above code will create a for loop
WRITING TO FILES :::
Experimental Code ++++
>>> file_name = open("taste.txt" , "a")
>>> file_name.write(" This is a new lines")
>>> file_name = open("taste.txt" , "r") >> putting back into readable format
>>> print(file_name.read())
We need to very careful while appending or editing a file as we can have it messed up easily
.
The above code will append in the next line of the file.
Overwriting a file :::
>>> file_name = open("taste3333.txt" , "w") The word w will overwrite a file. This command creates a new file.
Creating a new file :::
The above code will create an html file with the paragraph below that you code in html.
MODULES AND PIP ::::
A Module is a python file that you can import into your current python file, for instance if I have already written a python code with multiple useful function and variables extra we can indeed import that code into a new code and use the functions in the new code.
There is a python code that the programmer has written which he wants to import into his current python file that he is working on.
This is the way to import the other code into your current program where in you should be able to use all those functions and program
Now python is smart enough to understand that it has to fetch all the stuff from the file useful_tools
When you type in print(useful_tools. >>You will fins all the functions that are listed in the file useful tools here. Therefore you can run that function.
Where to get huge list of modules that you can import into your program ::::
type in "list of python modules" in google
https://docs.python.org/3/py-modindex.html
if you are looking for a python code, perhaps you can look for "Python module to do" perhaps someone has already written a code of what you are looking for.
Where are these modules stored that we see on the python website ::::
>>> There are these build in modules in python so that you directly have accesses to them
>>> And there are extenal modules are stored in the same folder where we install python
External Libraries can be found in the left side of the screen in PyCharm
There is a folder up there called "Lib" >> THIS IS THE ONE WHICH STORES EXTENAL MODULES. The build in modules are not stored here.
When you click on any of the modules in the python website you can find where is this module located in python :::
Source code: Lib/argparse.py
There are tons of modules, and it can take you years to peep into each one of them if you were to learn what each of these modules do.
A lot of times you want to use Modules that other people have written, there are loads of developer who work on python and have written a loads of modules.
You can install those third party modules that does not come with python, lets see how to install such external modules.
Google :::::::Key word :: useful thirdparty python module
Python-docx ::: Can be installed using PIP which is a package manager (package manager enables you to install , manage , update and uninstall) .
If you have a newer version of python the PIP should come pre-installed. And if you do not have a newer version of Python you may have to install PIP separately.
check if the PIP is already installed in python
pip --version
CLASSES AND OBJECTS ::::
Classes and Object in python shall help you to make your program more organized and powerful.
in python we are dealing with all types of data.
Nor all data can be represented using , string, boolean and int, there are loads of things in the real word that we cannot represent as a string or a boolean etc for example, things like a Phone , a person or a computer cannot be represented in the datatype that the programming language. In other words the data types that we have in python can't cover that
With classed and Objects we can create our own data type :::: So I can create my own data type for anything I want in python.
for instance : I can create like a "Phone" datatype and I can represent a phone. So that i can store all the information that I ever wanted to store about my phone inside of that data type and in Python I can create a class for that, with class you can essentially create your own data type and it is super awesome and classes are extremely helpful.
Classes are used in every single major programming language that use there.
Basic Introduction of Classes :::
Lets say I am writing a program for a college or a university and I want to model a student. I want to model a real world object and it is a student. I don't have a student datatype , string or a number
for instance I can create a class called Student, Basically creating like a student data type.
Create a New file names student.py
Class Student :
{ Anything that is intended here becomes the attributes of the class student.
We will create an initialize function
what we are going to have inside of this function is we can map out what attributes a student should have.
The above will tell you the student name, major, grade_point_average, is on probotion
this is defining what a student is in our program
The below will not make any sense now but in a while it will tell you what it is all about.
The above is a template of what a student is::::
Now we come to the app.py file leaving the student.py file .
Now we can actually create a student and give it some information that is called an Object
A class is defining what a student is , A Object is the actual student.
In order to use that student class and create a student object we actually need to import that
from the student.py file import the student class
The above will print out the name of the student1 object
BUILDING A MULTIPLE CHOICE QUIZ ::::
In this session we will have the user take the quiz and we shall keep track his points and finally tell him what his score is.
This little array is called Question Prompts
We have to keep track of the actual Question itself and the Answer to it, so what we are going to do is create a question class. we are going to create a little data type for questions
==
===complete program
OBJECT FUNCTION ::::



















































































Comments
Post a Comment