python 3 programming

Download Python 3 Programming

Post on 14-Jul-2016




4 download

Embed Size (px)




  • Python 3 Programming

    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"

    each_car Garage: for in

    (each_car) print

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

    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.

    What can Python do?

    Python is a fully-functional programming language that can do anything almost any other language can

    do, at comparable 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 task 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


    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.

    Print Function and Strings

    The print function in Python is a function that outputs to your console window whatever you say you

    want to print out. At first blush, it might appear that the print function is rather useless for

  • programming, but it is actually one of the most widely used functions in all of python. The reason for

    this is that it makes for a great debugging tool.

    "Debugging" is the term given to the act of finding, removing, and fixing errors and mistakes within


    If something isn't acting right, you can use the print function to print out what is happening in the

    program. Many times, you expect a certain variable to be one thing, but you cannot see what the

    program sees. If you print out the variable, you might see that what you thought was, was not.

    Next up, strings, what are they? Strings are just "strings" of text, hence the name. Strings are a type of

    data. Another type of data is integers.

    print('Single Quotes')

    print("double quotes")

    We're printing out a string. Notice that the quotes are single quotes. You can use single quotes or

    double quotes, but they need to be used together.

    While we're talking about strings and the print function, it would be useful to discuss concatenation.

    Concatenation just means the combination of things. You can use the "+" or the "," to join strings

    together. If you use a ",", then you will have a space in between the strings you joined. If you use a "+",

    then the strings will be strung together with no space. You will need to add one if you wanted.

    If you use the "+" to join integers and floats together, then you will perform an arithmetic operation. If

    you use the ",", then it will print them out separately, with a space.

    print('can do this',5)

    print('cannot do this:'+5)


    It is also important to bring up how to put quotes within strings. You can either put double quotes inside

    single quotes, singles inside doubles, or use the "\" backslash. The \ character is known as an escape

    character, and it will "escape" the characteristic of the following character and just take on the 'visual'

    aspect of it.

    The purpose of the "escape character" is to escape various characteristics for characters. For example, a

    quotation, ", in a string might wreak havoc. Take for example: x = "He said, "Hello there!" "

    Yeah, that's going to be a problem. There are obviously many options to avoid this specific problem, but

    one of them would be to use an escape character:

    x = "He said, \"Hello there!\" "

  • If you do a print(x), you will not see the escape characters, and you will see the quotes. Sometimes you

    want to show the escape character as well:

    x = "An escape character is a \"

    How might you solve that?

    Here are some examples of quotation rules:

    print('Can't do this')

    print('you\'ll have success here')

    print("you'll have success here too")

    It is also important to bring up how to put quotes within strings. You can either put double quotes inside

    single quotes, singles inside doubles, or use the "\" backslash. The \ character is known as an "escape"

    character, and it will "escape" the characteristic of the following character and just take on the 'visual'

    aspect of it.


    # this is a single line comment

    You can also do multi-line comments like:


    Multiple line comment, can use triple "double quotes" as well to do




    This is a massive print, where you want to

    use multiple lines, maybe make designs, or

    something like that.


    Getting User Input For a simple text-based GUI (graphical user interface), it can sometimes be useful to allow for a user to

    enter some input into the program while it runs. Using Python 3's "input" function, we can do that.

    In time, you may want to eventually learn how to make GUIs in windows, but you will still find yourself

    needing raw input from time to time, like text fields, even in these sorts of GUIs.

    x = input('What is your name?: ')


  • If you enter your name, the output will be a hello and your name.

    Calculator: In Python 3 doing math is very well works and extremely simple. As like using simple calculator.

    Variables In almost every single Python program you write, you will have variables. Variables act as placeholders

    for data. They can aid in short hand, as well as with logic, as variables can change, hence their name.

    Variables help programs become much more dynamic, and allow a program to always reference a value

    in one spot, rather than the programmer needing to repeatedly type it out, and, worse, changes it if

    they decide to use a different definition for it.

    Variables can be called just about whatever you want. You wouldn't want them to conflict with function

    names, and they also cannot start with a number.

    You want to be careful what you name variables, classes (discussed later), and functions (discussed

    later), so that they do not have the same names as each other.

    For example, you have learned about the print function. What if you go and define a variable named


    Say, for example, you do:

    print = print("Uh oh!")

    Now you have a variable and a function named print, which can cause trouble down the road!

    NewVar = 91


    In this case, we will have a 91 printed out to console. So, in this case, we were able to store an integer to

    our variable.

    cannotDo = Hey!

    Hey! is not a valid datatype, and this will throw an error. You would need to throw quotes around the


    canDo = 'Hey!'


    canContainOperations = 5/4


  • Here, we can see that we were even able to store the result of a calculation to our variable.

    We can even store a variable to our variable or an operation with our variables to a variable. Something

    like var3 = (var2/var1) would work. You can store other things, like functions, as well to variables.

    Loops and Statements In this section we are going to see about while, for loops and if statements. The two distinctive loops we

    have in Python 3 logic are the "for loop" and the "while loop." Both of them achieve very similar results,

    and can almost always be used interchangeably towards a goal. Many times it comes down to

    programmer preference, or is reliant on efficiency. Generally, for loop can be more efficient than the

    while loop, but not always.

    While Loop

    The idea of the While loop is:

    While something is the case, do the following block of code.

    Here is an example of a while loop:

    condition = 1

    while condition < 10:


    condition += 1

    Next, we specify the terms of the while statement, which are: While the condition variable is less than

    10, we will print the condition variable out. After printing out the condition, we will add 1 to the current

    condition. This process will continue until condition equals 10.

    This setup of a while loop is known as creating a "counter," since basically that is what we're doing.

    We're saying we just want to count 1 for every iteration and eventually stop at our lim


View more >