Python programming 3

Before we begin talking about math, let's talk about why we are using python. Python is a very high level language meaning most of the words we see in it are plain english and generally easy to understand. Other C-type languages can be difficult to read and look complex. Python offers everything to you in easy to understand ways. How can it do this? Python like other languages sits on top of other code. A language like C++ is sitting on top of machine code which you can't read. We use C++ because it's easier to read than machine code. A language like Javascript sits on top of web browsers and needs a browser to interpret the code for the window. Python sits on top of C++ in a unique way. It is wrapped around C++.

I'm going to inject several paragraphs from Harrison Kinsley, a great python programmer.


Why Python?

Python is very beginner-friendly. The syntax (words and structure) is extremely simple to read and follow, most of which can be understood even if you do not know any programming. Let me show you:

Garage = "Ferrari", "Honda", "Porsche", "Toyota"

for each_car in Garage: print(each_car)

"print()" is a built-in Python function that will output some text to the console.

When someone refers to "the console," they are referring to where information from your program is ouput. You will see an example of "output to console" below. If you want this message to go away, just click again on the "console" button that you originally clicked on.

Whenever you see a word encased in blue like "console" is above, you can click on it for more information, and click it again to make the information disappear again. Any words or phrases that may not be known by everyone will have this functionality.

Looking at the code about cars in the garage, can you guess what will happen? You probably have a general idea. For each_car in the garage, we're going to do something. What are we doing? We are printing each car.

Since "printing" outputs some text to the "console," you can probably figure out that the console will say something like "Ferrari, Honda, Porsche, Toyota."

What can Python do? Python is a fully-functional programming language that can do anything almost any other language can do, at comprable speeds.

Python is capable of threading and GPU processing just like any other language. Most of the data processing modules are actually just Python wrappers around C/C++ code.

"Modules" are pre-written Python code that you "import" in your Python program. Since there are many tasks that people commonly do, we have modules that people have written that do these taks for you, and they usually do them in the cleanest and most efficient method possible. Sometimes you will see people refer to "DRY." This stands for Don't Repeat Yourself, which often also translates into "Don't Repeat Someone Else."

The phrase "wrapper" means that someone has placed, like a wrapper, Python code over another language. So, when you have a Python wrapper around C++ code, what someone has done is written some Python code that interacts with the C++ language. This allows you to make use of various aspects of the language being wrapped, in this case C++, without actually needing to know or understand that language.

Thus, Python can be used to make games, do data analysis, control robot and hardware, create GUIs, or even to create websites.

"GUI" stands for Graphical User Interface, and is used to describe a program that incorporates graphics to make the program more interactive for the user.


Just like when you were adding two numbers together, you can subtract, multiply, and divide numbers. Try out:

print(5-3)

easy. Try this:

print(7*7)

Obviously 49. This is kids stuff.

print(10/4)

This is clearly 2.5.... hey wait a second. So every other mathematical operator works in programming except division? What kind of sorcery is this! There's actually a very simple explanation. The reason why you get 2 when dividing 10/4 is because you are dividing integers. What are integers? Whole numbers. Because of this the computer expects you want a whole number back. So it rounds down from 2.5 to 2. One way around this is to write:

print(float(10)/float(4))

Floating point variables are created when you wrap numbers with the float() function. Now any number put in will become decimal. Try this out with some examples.

Similarly, if you wrote:

print(int(2.5))

The answer you get back will be 2.5 converted to a whole number. So 2.

Another operator that you probably aren't familiar with is the modulo. (%)

Modulo is just like division. Instead of dividing out two numbers and figuring out how many times 10 goes into 3 (answer=3), modulo gives you the remainder (1).

print(22%4)

The print out is 2 because 4 goes into 20 five times but there is a remainder of 2. Modulo is very helpful and is an important computer science concept to understand.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Video tutorials Video 1


Now your task is to make a program that takes two numbers from the user and figures out what the remainder is when the first number is divided by the second.

Your next project is to ask the user for initial velocity, time, and acceleration and return the distance an object traveled back to the screen.

distance = init velocity time + 1/2 a time time

results matching ""

    No results matching ""