Pages

Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts
Wednesday, January 24, 2018

Control Flow Tools for Python

With the inclusion of strings into our knowledge of Python we can now begin to use tools within Python to not just calculate arithmetic's but use statements to solve on its own. We will begin with the two basic statements "If " and "For", then in a follow up blog we shall work into the Range functions  as well as break, continue and else statements.

If Statements 


One of the most well known statements in python and programming in general is the "If" statements. Providing conditional execution. Pretty simple Right? Lets check it out!

Example of an If statement from the book

Keep in mind at the end of the block you can see an "else:" statement, that will be shown a little later however here is my take on the "If" statements!



The reason why "More" has been printed is because of 20 being greater than 0, if it was equal to 0 it would print the word "Zero" and if it was 1 the program would have printed "Single". A very good example of an If statement.

For Statements


As stated by Guido " The for statement in Python differs a bit from what you may be used to in C or Pascal. Rather than always iterating over an arithmetic progression of numbers (like in Pascal), or giving the user the ability to define both the iteration step and halting condition (as C), Python’s for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence. " (Guido Van Rossum 4.2).  Here is an example from the book of a "for" statement:
The for statement
The program written here tells Python to count how many letter are in the words cat, window, and defenestrate which returned with the numbers 3, 6, and 12 respectfully.
Here is my take on the program :


I tested the books program first to see is if the computer would count from 0 or from 1 and it starts from 1 for python unlike web designing languages. After recreating the books program I put my own words and decided to test a couple of other words for fun and the program sent me the correct numbers for the words!
We will dive down into the range functions and other tools later, as for now I have a question for everyone again. With the high demand for knowledge of computers, will IT and similar programming jobs take over engineering and other popular jobs that used to be in demand?









Wednesday, November 29, 2017

Calculating using Python

Calculating using Python 3.6.3

Python is an easy to learn, powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. Python’s syntax and dynamic typing, together with its interpreted nature, make it an ideal language for scripting and rapid application development in many areas on most platforms. Today I will show how to use Python as a Calculator and show some interesting features as well.
"The Interpreter (software) acts as a simple calculator: you can type an expression at it and it will write the value. Expression syntax is straightforward: the operators +, -, * and /  and parentheses (()) can be used for grouping," (Guido Van Rossum 3.1). Here is a line from the book showing each of these expressions.
Here are my expressions
For any expression written that would have a decimal throughout the solving of the expression or the answer itself. They will always return a floating point number. 

I would suggest practicing the basics as when you learn them you can interpret more advanced expressions for example: I turned 25 Kilograms into how much it weighs in stone!

My question for all of you guys is "Have you tried any computer languages? which ones?" 

Michaels, Evan. Rossum, Guido Van. Learning Python. Python Software Foundation, 2001-2017


Tuesday, January 31, 2017

Python: TurtleWorld and Polygons

In this blog post, I will be continuing on with the TurleWorld and seeing if I can make different shapes and objects using my previous knowledge and guidance from the book. For example, in the first exercise, it wants me to simplify my coding by adding a “for” statement. “A for statement is sometimes called a loop because of the flow of execution runs through the body and then loops back to the top” (Downey 39). In this case, I used the “for” statement to simplify and make my square coding not as repetitive. 



In this example, I had the line of code that I used in the last post to repeat 4 times, instead of me typing it out 4 times. In the next exercise in Think Python. It wants me to fiddle around with the angle of the turns that the turtle can make. In this case, by making the polygon a function, I can change around what I can input in its parameters to change the shape that I make with the Turtle. In the example that Think Python gives me, the first line of code defines the function “polygon” to have the variables, t, n, and length. Then in the function of the polygon, there is a variable called angle, which calculates the exterior angle of the polygon. Then there is a loop statement again that moves the turtle (t) in a certain length, and then turns the turtle left in a certain angle. The last line of the code executes the function “polygon” with 7 sides with each side length being 70 (Downey 39).





During the next examples in Think Python there was also a challenge to program it so that the turtle draws a circle. While I tried my best to figure it out I eventually ran out of time and gave up to be able to post on the blog on time.

Question: When have you given up on something because it was too difficult?


Downey, Allen B. Think Python: How to Think like a Computer Scientist. Sebastopol: O'Reilly Media, 2015. Print

Python: Modules and TurtleWorld

 Most programming languages include modules in them, like Python. “Modules [are] a file that contains a collection of related functions” (Downey 24). For example, there is already a built-in module in Python called “math”. The math module imports lots of functions into the program so that you can reference them in your code. An example of this would be when you import the math function, you can access difference new functions such as square roots, pi, and logarithms.



In the book “Think Python, Chapter 4” it tells to download a package called “Swampy”. Packages are just a collection of modules that you can install, since modules are collections of new functions, packages are in essence adds lots of new functions into the programming language (Downey 37). In Swampy there is a module that is called TurtleWorld. Using this you can create a “TurtleWorld” which is a window where a “Turtle” will draw what you tell it to draw. For example in the code below, the first line imports all the functions from the TurtleWorld module from the Swampy package. Then the next lines in the script make a TurtleWorld called world, and a Turtle called bob. Using this we can use bob and the TurtleWorld to draw a square and other shapes as we want. The “fd” function moves the Turtle “bob” forward 100, and the “lt” function turns him to the left. Repeat this 3 more times and we have a square.




From this experience, I was surprised the capabilities of Python and what it can accomplish, specifically with the TurtleWorld modules.
 Question: What are some things that you were surprised you can accomplish?
Downey, Allen B. Think Python: How to Think like a Computer Scientist. Sebastopol: O'Reilly Media, 2015. Print
Sunday, January 29, 2017

Lists Done With Functions in Python

In the book Python Crash Course I learned how to use functions in Python and implement them into lists. It explains this on page 134, "When you want to perform a particular task that you've defined in a function you call the name of the function responsible for it." (Matthes 134). This taught me how to use functions in Python which simplifies my code to be more clean.
I also learned how to format my text with functions. This is explained on page 141, Functions can store values and format them to be more neat (Matthes 141). This taught me how to change format values I already have with a function which diversifies what I can do in Python.
How would you create your lists in Python?

Matthes, Eric. Python Crash Course: A Hands-on, Project-based Introduction to Programming. San Francisco: No Starch Press, 2016. Print.

Lists Done With a While Loop in Python

In the book Python Crash Course I learned how to use while loops in Python and apply it to lists. It explains what while loops are on page 122 it says, A while loop is a loop that runs as long as a value set in the loop is true. (Matthes 122). This taught me how to easily implement while loops into my code which helps me learn more of what I can do in Python.
I also learned how to stop a while loop before its condition is false. This is explained on page 125, "To exit a while loop immediately without running any remaining code in the loop, regardless of the results of any conditional test, use the break statement" (Matthes 125). This taught me how to implement a statement that can stop the loop which helps me improve at programming in the Python language.
What other programming language should I learn about?

Matthes, Eric. Python Crash Course: A Hands-on, Project-based Introduction to Programming. San Francisco: No Starch Press, 2016. Print.

List Done With a Dictionary in Python

In the book Python Crash Course I learned how to use dictionaries in Python and apply it to lists. It explains this on page 96, Dictionaries are a collection of pairs, the first part of the pair is a name while the second part is the value the name is associated with (Matthes 96). This taught me how to utilize other methods to create lists which helps me diversify how I program in Python.
I also learned how to add new pairs to dictionaries after making it. It also explains this on page 97, "To add a new key-value pair, you would give the name of the dictionary followed by the new key in square brackets along with the new value" (Matthes 97). This taught me how to improve upon dictionaries which helps me improve at programming in Python.
What program should I do next in Python?

Matthes, Eric. Python Crash Course: A Hands-on, Project-based Introduction to Programming. San Francisco: No Starch Press, 2016. Print.

Saturday, January 28, 2017

Python:Functions

In Python and in almost all programming languages there are these commands called functions. “In the context of programming, a function is a named sequence of statements that performs a computation” (Downey 23). This is very useful in programming since if there is a repetitive sequence of code that we want to execute, instead of repeating the same lines over and over again we can make a function for that sequence of code so that we can call on it when we need to. In Python, there are already many different functions that are built into the program. However, there is also a way where we can add a function into our script. This is called a function definition. When we define a function and its sequence of code that executes whenever the function is being used (Downey 25).   
An example of this is







In the code, there is a keyword called “def”. This means that this is a function definition, the keyword "def" defines the line of code as a function. The text that is next to the “def” is the name of the function. In this example, the name of the function is called “print_lyrics”. Finally, the text under it is what the function contains and what it will do when it is executed. In this example, this function will print out the lines. Finally, the line of code at the end of the function executes the function (print_lyrics()). (Downey 26).
“I’m a lumberjack and I’m okay.”
“I sleep all night and I work all day.”
 Using this new found knowledge we can write a program that can draw a grid.
In this program, I made two functions that printed out a line of the grid. Then making a final function I printed out the grid.









Question: When have you found yourself having to do something repetitive?

Downey, Allen B. Think Python: How to Think like a Computer Scientist. Sebastopol: O'Reilly Media, 2015. Print.
Saturday, December 31, 2016

Python-Interactive Mode and Script Mode

       In Python, there are two ways to code, one way in Interactive Mode and another way in Script Mode. While there are advantages to each both of them, there can be some disadvantages too, this is why you need to utilize both of them to be successful. The advantages to using Interactive mode is that you can test out bits of code to see if they work before you put them into your script. In script mode you can store multiple lines of code so that you can execute it all at once. There are also differences when you are coding in interactive mode and script mode that can also be confusing (Downey 17).
For example:

If you code this into the interactive mode then the output would show up under it
X=126
X*420






However if you type this exact code into script mode the output would be different


As you can see on the left side is the script, and when executed there was no output on the right, or the shell. This is because we didn’t tell Python to print out the output which is sometimes confusing as stated before. “But if you type the same code into a script and run it, you get no output at all. In script mode an expression, all by itself, has no visible effect. Python actually evaluates the expression, but it doesn’t display the value unless you tell it to:” (Downey 17).
  



Since we added the print command in the script there was a visible output like in the interactive mode.

Question: What are some other scenarios where you need more than one resource to be successful?

Downey, Allen B. Think Python: How to Think like a Computer Scientist. Sebastopol: O'Reilly Media, 2015. Print. 



More in Depth Lists in Python

I learned how to develop my lists even more and make them more in depth. In the book Python Crash Course it showed me how to use if statements, on page 76 it said, You can change specific elements by using an if statement. (Matthes 76). This taught me how to change specific elements in a list to be more in depth and precise which leads to me being better at programming in the Python language.
Another thing the book showed me how to do was display user input, on page 118 it said, "The input() function pauses your program and waits for the user to enter some text. Once Python receives the user's input, it stores it in a variable to make it convenient for you to work with." (Matthes 118). This taught me how to implement code that can interact with the user which helps me become better at programming in the Python language because interactive programs are very useful.
What types of programs do you think are most useful?

Matthes, Eric. Python Crash Course: A Hands-on, Project-based Introduction to Programming. San Francisco: No Starch Press, 2016. Print.

Wednesday, November 30, 2016

Lists in Python

Matthes, Eric. Python crash course: a hands-on, project-based introduction to programming. San Francisco: No Starch Press, 2016. Print.
I learned how to make lists in Python and display them in multiple ways. In the book Python Crash Course it showed me how to reverse lists, on page 49 it said, You can reverse lists by using the name that defines a list and adding .reverse() to it. After that line you type print(name) and it will be printed in reverse. (Matthes 49). This taught me how to implement reversed lists into my code which helps me improve at programming in the Python language. Another thing the book showed me how to do was separate list items, on page 38 it said, "Write the name of the list followed by the index of the item enclosed in square brackets." (Matthes 38). This taught me how to implement a list into my code that looks more professional when printed which helps me improve at programming in the Python language.

What do you think the most important programming language is?

Matthes, Eric. Python Crash Course: A Hands-on, Project-based Introduction to Programming. San Francisco: No Starch Press, 2016. Print.