Sunday, October 19, 2014

Intro to Python: Getting input

Intro to Python: Basic input

In my previous tutorial, I went over how to print out basic texts to the terminal. That's all fun and games, but what if we also want to receive input from the user as well? Let's say, for the sake of example, that we are making a text-based RPG and we wish to have the player enter a name for our hero. This is done in python by using a method called raw_input().

To use our previous example, one thing I could do is this:

print("What is your name?")
name = raw_input() 

Now, when we run this program in the codeacademy labs, we should get this as the output:

Code Explanation


 As you can see, I got the output "What is your name?". On a new line, I inputted the name. I used my name Chris here as an example, but you can put in whatever name you want.

Now, let's go through the code each line.

print("What is your name?")

This line will output the text "What is your name?" to the screen. 

name = raw_input()  

This line is getting input from the user. You may have noticed that I am making a variable called name and assigning it to the input. Don't worry about that too much right now, just know that now the variable name is equal to whatever input I got from the user. 

 String Concatenation

I can add an extra line of code such as

print("Hello " + name + "!")  

to the end of the code. Now, when I run the program I get this output:


 In the last line of code, what I did is concatenate the strings with a variable. This sounds complicated, but it's actually very simple. Concatenation just means you are adding to things together. In this case, I am adding the strings together.

To give another example of this, I could do something like:

print("Hello " + "World" + "!")  

This will give me the output:


See? Simple! Try getting the input from the user with new variables now to see if you fully understand. If the program doesn't run correctly the first time, don't worry. Just double check your program to see if there are any simple mistakes. If you still can't get it, reread this tutorial or find other examples of code online to help you.

In my next tutorial, I will be going over if and else statements and using them with variables.


 
 

 

Thursday, October 16, 2014

Introduction to Python

An intro to the Python programming language

What is Python?

The Python programming language is a general programming language that can be used for many different purposes. Unlike C++ or Java, python is quick to set up and useful for teaching neophytes how to program. Python emphasizes readability instead of terseness. So, how exactly can we run a Python program?

Running a Python program

You can do one of two things: Download a python interpreter or use an online python interpreter. If you wish to do the former, you are on your own. However, if you want to do the latter then you can use Code Academy labs to run your python programs.

http://labs.codecademy.com

Click on the link above and select python. You should get a screen that looks something like this




Printing strings to the terminal

Now, the most basic program that we can create is a "Hello World"
type program. Now, on the left side of the screen put the following code:

print("Hello, World!")

Then click run. You should get the words Hello, World! on the right side of the screen.

In my next tutorial I will talk about how to get basic input from the user.


 

Saturday, October 11, 2014

LaTeX: Lists

Lists

In my last tutorial, I stated that I would go over lists in LaTeX. Well, here is how to create lists in LaTeX!

Bullet Points

Making bullet points in LaTeX is very simple. Within the \begin{document} and \end{document}, put \begin{itemize}. Texmaker should auto complete that to add \end{itemize} as well. Now, in between itemize put in \item and add whatever you with to put in bullet points. For example, 

Variable Types
\begin{itemize}
\item float
\item boolean
\item char
\item double
\end{itemize}

This will produce:


 This is fine if we want only one level, but what if we want sub-points? This is done by adding another {itemize}. If we were to categorize variable types by whether they deal with numbers, letters, or other we could do this:

 
Variable Types
\begin{itemize}
    \item Numbers
    \begin{itemize}
        \item byte
        \item integer
        \item decimals
        \begin{itemize}
            \item float
            \item double       
        \end{itemize}
    \end{itemize}
    \item Letters
    \begin{itemize}
        \item char
        \item string
    \end{itemize}
    \item Truth values
    \begin{itemize}
        \item boolean
    \end{itemize}
\end{itemize}
\end{document}

This code will produce the result:

 
 Let's say, however, that you want to make numbered lists. 

Numbered Lists

  

Creating numbered lists is similar to creating lists with bullet points in the sense that they both use \begin and \end. However, while lists with bullet points use {itemize}, numbered lists use {enumerate} 
Let's say, for the sake of example, I wanted to create a list of programming languages I am proficient in on a resume. I could do this:

 Programming Languages
\begin{enumerate}
    \item Python
    \item Ruby
    \item C++
    \item Objective-C
    \item Java
\end{enumerate} 


Giving me the output:



Lastly, we can combine both numbers and bullet points by inserting the \begin{enumerate} within a \begin{itemize}

Going back to the example I had before, assuming that I wanted to separate these programming languages by bullet points I could do:

\begin{itemize}

\item Scripting Languages
    \begin{enumerate}      
    \item Python
    \item Ruby
    \end{enumerate}
\item Compiled Languages
    \begin{enumerate}
    \item C++
    \item Objective-C
    \item Java
    \end{enumerate} 

\end{itemize}


This will end up giving us the output:



That's the end of today's lesson. In the next lesson, I will go over how to set up a TeX document for a lab report.