Author Archives: Jorge Padilla

ECOS

Hey there! I just finished evaluating my teachers of this semester. You should do the same in order to give them some feedback! Here’s the evidence I already did it:

Quiz #9

I corrected my files for partial exam 2. I asked some of my classmates to explain to me how they solved it in order to have my code right. And here they are, check them out!

Question 1: https://github.com/yorchpave95/Quiz-9/blob/master/q1.py

Question 2: https://github.com/yorchpave95/Quiz-9/blob/master/q2.py

Question 3: https://github.com/yorchpave95/Quiz-9/blob/master/q3.py

Question 4: https://github.com/yorchpave95/Quiz-9/blob/master/q4.py

Basic output (print) in Python

I made a very basic program in Python that prints a string and a number.

“Print” is the most basic functions in phyton and is used to show anything you want in the console. For using it, it has some rules that must be followed, for example,after print always goes a parenthesis(), inside can bet integers, strings, and also text but the text has to be inside quotation marks “” for example: 

 

Use of “elif” with a conditional

I made a basic program in python with “elif” conditional. Check it out.

elif is short for ‘else if’ and is very helpful when you have too many if statements  in your code and makes your code more efficient and cleaner. You can use this conditional after an if in the next line and to use it you have to put your condition and it will make its job, it will make the action you define if the condition is True or the other action you define if the condition is not True, you can see it in my example:

Creation and use of strings in Python

I created a basic program in Python with “string” variables and made a simple example using them.

In phyton, numbers can be called integers or floats, the strings are everything else that is considered as a letter or a word and any operation can not be made with strings as it can with integers, to create strings you just have to enter str() , int() for integers and float() for floats, for example:

str(0)+str(6)       pyhton will print ——–>    06

int(2)+int(5)       python will print ——–>     6

Use of loops with “while”

I made a Python program with a basic “while” loop.

For using loops in phyton, you have to apply the while function, for using it you have to place a condition and if that condition is True, this loop will end and the program is going to do the next action after the while,while loops, like the for loop, are used for repeating sections of code – but unlike a for loop, the while loop will not run n times, but until a defined condition is met. As the for loop in Python is so powerful, while is rarely used, except in cases where a user’s input is required its important to know that the condition of the while has to be the opposite you want for the loop end. for example if we want to end the loop when the variable is equal to 0 we use:while(variable!=0):, but this wont include the 0 on the program, so we use while(variable>0):. It is important to know that the code that is going to be in the loop has to be indented, this means with 4 spaces or a shift distance from the left than the while, like in the example below.

Use of loops with “for”

I created a Python program with a basic “for” loop.

Loops can be created with while and for making loops with forFor loops are traditionally used when you have a piece of code which you want to repeat n number of times If you’ve done any programming before, there’s no doubt you’ve come across a for loop or an equivalent to it. In Python, they work a little differently. Basically, any object with an iterable method can be used in a for loop in Python. Even strings, despite not having an iterable method – but we’ll not get on to that here. Having an iterable method basically means that the data can be presented in list form, where there’s multiple values in an orderly fashion. You can define your own iterables by creating an object with next() and iter() methods. This means that you’ll rarely be dealing with raw numbers when it comes to for loops in Python – great for just about anyone!.

source: wiki.python.org

Creating Python functions

To create a function you have to give your function a name after the word “def” and then, in parenthesis, specify the variable you’re going to work with within that function. After that, you want to write what you want the program to do in return whenever you call that function. For example:

def hello(x):

       return “Hey there ” + x

And there you go. You’ve created a new function!

Calling Python functions

I made a simple Python program where i call a function i just created in that same Python program.

To call a function, first you have to create one(You can see how to here) then you give the values or ask the user to input value or values to the function to work, the next step is to create a new variable and asign it the function, now you are up to call the function using a print you can print the valiabre you assigned to the function, it will print the result of the function with the numbers you or the user gived.

If you don’t know how to create a Python function, you may want to go and check the post I made about it! Here’s the link: http://jorgepadilla95.withknown.com/2015/creating-python-functions

Creation and use of lists in Python

I used the Python IDLE where I create and use a very simple list. Check it out.

To create a list you just have to type a name and in [] the things you want in the list, like this list=[1,6,5,4,]each of the components of the list has to be separated with a comma or they will be recognized as one component, the lists components have numbers, the first one is the number 0, ne next one 1 and like that untill the end, the last component also could be -1, the one before -2 and like that until the first one. to use list this commands has to be known.

list.append(x)

Add an item to the end of the list. Equivalent to a[len(a):] = [x].

list.extend(L)

Extend the list by appending all the items in the given list. Equivalent to a[len(a):] = L.

list.insert(ix)

Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x).

list.remove(x)

Remove the first item from the list whose value is x. It is an error if there is no such item.

list.pop([i])

Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)

list.clear()

Remove all items from the list. Equivalent to del a[:].

list.index(x)

Return the index in the list of the first item whose value is x. It is an error if there is no such item.

list.count(x)

Return the number of times x appears in the list.

list.sort()

Sort the items of the list in place.

list.reverse()

Reverse the elements of the list in place.

list.copy()

Return a shallow copy of the list. Equivalent to a[:].

Source: docs.python.org