Showing posts with label intro. Show all posts
Showing posts with label intro. Show all posts

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. 

Friday, September 26, 2014

Starting out in LaTeX

Part 1

Now that we have texmaker on our computer, we can begin to create Tex documents!

Today I am going to introduce to you to a basic "Hello World" type program that will show you basic concepts in TeX. 

Open up texmaker. When you open it up, you should see something like this:

To create a new file you can do one of two things:
  • Go to File -> New
  • Ctrl + n
Once you have created a file, type in the following code into the right window:

\documentclass[12pt]{article}
\begin{document}
Hello, \emph{World}!
\end{document}


Now, save the file wherever you want and then go to Tools -> PDFLaTeX. This will convert the file into a PDF file so that other computers can see it. Go into the folder where you saved the TeX file, and you should find a PDF there. When you open it, it should look something like this:


 
As you can see, we now have a PDF that shows the words "Hello, World!" but...

How does this code work exactly?

Code explanation

The line \documentclass[12pt]{article} declares what type of document type it will be. Here, the document type is being declared as an article. This line also shows what the font size will be. This line makes the font size 12pt font, but we can edit this to be either 10, 11, or 12. So I could have written:

\documentclass[10pt]{article}

\begin{document} and \end{document} should be fairly self explanatory. We are designating the beginning and the end of the document. Anything within these two lines will be put into the document. 

Hello, \emph{World}!

Here, we are putting Hello, World! in the PDF.

The portion that should be noted here is \emph{World}! 
  
As you may have guessed, \emph is short for emphasis. Anything between the braces will be emphasized. In other words, they will be italicized. However, this is not the only way to italicize text. Text can also be italicized by using \textit{} where, again, everything between the braces are italicized. 


Hopefully you learned some more about LaTeX. In my next tutorial, I will go over how to make lists in LaTeX as well as basic formatting. The next tutorial will be longer than this, so it will be up by Sunday night. 

Thanks for reading. If you liked this tutorial, make sure to share this blog with others.