A Beginner’s Git and GitHub Tutorial

Beginner's Tutorial to Git and GitHub

Git and GitHub are two of the coolest technologies around for developers. Git, despite its complexity and rather terse beginnings, is the version control tool of choice for everyone from web designers to kernel developers. And GitHub is the social code-hosting platform used more than any other. On GitHub, you’ll find everything from playful, simple experiments to the Linux kernel itself.

But despite this popularity, there’s a lot to learn if you want to use these tools properly, and not just be a beginner. Both tools are sophisticated, providing a rich tapestry of functionality. Consequently, they’re not for the faint of heart: they can be quite demanding if you want to use them to the fullest.

So if you’re just starting out, perhaps coming across from one of the older version control tools, I want to help you make a great start, by giving you a solid working foundation from which you can grow your knowledge over time. To do so, we’ll start with Git, learning how to perform the common operations you’ll do every day on a local source code repository. Then we’ll cover the basics of GitHub, and how to integrate the local Git repository into it, so that others have access to this project as well. Sound good? Then let’s get started.

Continue Reading

4 Ways to Pick Your First Programming Language

If you haven’t picked your first programming language, the programming world is your oyster. Yet with evangelists for every language telling you their language is the best, choosing one to start with can be incredibly overwhelming. We’ve looked at the data for the top ten programming languages in the US (based on IEEE Spectrum data) to help you pick the best language to start with based on your priorities in lifestyle, location, and career potential.

Python is a popular, well-paid language, being versatile enough to be used in many different applications, while Javascript is used widely across the country, and can be a good choice if you don’t want to relocate for a job. Although some newer programming languages, such as Swift, are not included, you shouldn’t discount the growth of their popularity. Career opportunities in iOS development using Swift, similar to Android development using Java, will increase as the field of mobile app development continues to expand.

Continue Reading

Learn Programming? You Already Know How.

Programming.

Programming is a fantastic career field for many reasons (great pay, solid career growth, serious job demand). No news there. But what you may not realize about programming as a job choice is that, no matter how far along in your studies you may be, you can actually already do it really well.

“Say what?!” you may be thinking, if you’re a total coding newbie and intimidated by the perceived learning curve. Or else, if you’re in the process of picking it up or sharpening your skills, “I know I can already do it, I’ve learned through courses, books, blogs, and hours of coding practice.” But here’s the thing: every day, people of all types, in all fields, of all mathematical abilities, are “programming” in their daily lives simply by way of using certain thought structures or patterns of logic. Computer programming is just the practice of transcribing those everyday rules into a more structured format.

Continue Reading

3 New Programming Languages You Should Know

Go, Rust, Swift. 3 new programming languages you should know.

The world of programming languages is vast. Languages that are tried and true often dominate the landscape (C, Java, PHP), with contenders stepping into the arena every so often (Scala, D). However, due to the nature of software development, it can be hard for new languages to gain traction. You generally need a strong reason to switch from a language you’re currently using: it requires time to practice and learn new languages, and you have to be confident that the language you’re considering switching to will be supported in the long term. Nobody wants to build software in a language that will be obsolete in five years’ time.

Continue Reading

iOS Resources Roundup: Edition 1

It’s the first edition of our iOS Resources Roundup, a monthly roll-up of our favorite articles, resources, and courses in the iOS world. Go ahead: pinch and zoom away.

Aerial view of a clean hip desk. iOS Developer Resources Roundup

From time to time, it’s fun to imagine a big break. Read how a small studio landed an app in an Apple ad during the World Cup.

Did you preorder? If so, this list of all upcoming Apple Watch apps will find comfort in your bookmarks bar.

The Art of Code Review: A Dropbox Story is a peek into tools and practices that let a team of developers fix code together.

Tick tock. Anyone can now submit Apple Watch apps and this is exactly how the WatchKit submission process works.

When designing apps, the best icon is almost always a text label.

How much money can indie app developers make? Overcast reports on revenue numbers from three app developers.

Apple’s Network Link Conditioner is a useful performance tool that you can add to your iOS development toolbox. Check it out.

Spice up Apple’s Xcode with these 13 tips and tricks.

Want to develop native iOS apps with JavaScript? It might be time to take a look at React Native.

If you’ve ever struggled with AutoLayout on iOS, here’s advice straight from an Apple engineer.

Core Intuition is a great podcast about Apple indie software development, and Episode 179 is about all things Apple Watch.

“A better job is one of the best kinds of recognition.”

“With the skills I gain I will be prepared for an entry level iOS developer role. But I will also be prepared for jobs of the future and can even apply these skills to my current role.”

— iOS Developer Nanodegree student Mikaila. Read more about why she’s taking up the Udacity program on Getting Smart.

New iOS Courses

UIKit Fundamentals with Gabrielle Miller-Messner — learn the iOS user interface library. Level: intermediate

iOS Networking with Swift — learn how to incorporate networking into your apps. Level: advanced

iOS Persistence and Core Data with Jason Schatz — techniques for storing data to your device’s hard drive.

iOS Developer Nanodegree — get notified when the next enrollment period opens.

Udacity’s Hidden Easter Eggs

Easter is right around the corner, so we’re sharing a few Udacity code Easter eggs for you to enjoy. Let us know in the comments if you’ve found others in our courses, or what your favorite Easter eggs are from around the web!

Developing Android Apps

If you get this Developing Android Apps quiz correct (click on the one checkbox), you’ll see a happy dancing Android.

android dance part 1

But the poor guy doesn’t get to finish his celebration, and you’re asked to find the other half of his dance. Never fear! Head over to this quiz from later on in the course, and the right answer (click both checkboxes) shows the little guy’s finishing moves.

Droid happy dance

android dance part 2

Programming Foundations with Python

This course video from Programming Foundations with Python teaches you how to open a web browser in Python so you can listen to some relaxing saxophone music while taking a break from all the learning. But before you doze off, take a closer look. If you copy the YouTube link from the video, you’ll realize you’ve found something much more upbeat. In fact, you’ll realize you’ve just been ‘rickrolled‘.

How to open a web browser in Python

Data Analysis with R

The next two Easter eggs may or may not reflect some of the frustrations you’ve felt on your programming journey.  It may help to know that by putting these tidbits in here, our instructors have probably felt the same way.

In this Data Analysis with R quiz, a correct answer of .948 will yield what we hope is a situation you don’t often encounter. But then again, we’re all human, so something like this probably happens more often than we care to admit!

Productivity vs deadlines

Developing Android Apps

The instructor notes in this video from Developing Android Apps describe a situation that may be all too familiar.

“You’re testing an app on your favorite phone device, and everything looks great, but then you rotate it into landscape, or run the same app on a larger phone or a tablet, and it’s the same UI just squeezed into the top left corner and you’re all like…

Android frustrations.

Website Performance Optimization

Most of our Easter eggs are embedded in our courses, usually when you submit the correct answer to a quiz. However this egg, found in Website Performance Optimization, is not one most people will find (we don’t think), since it’s not based on a correct answer. Don’t worry about whether each sequence of events is a ‘blocking’, ‘inline’ or ‘async’ script. Just know that you want to see a ‘fat’, ‘cat’, ‘breathing’.

Fat cat breathing.

There are other secrets tucked away in our courses, but we obviously can’t tell all. The rest are there for you to find! What other fun and interesting code Easter eggs have you discovered, on the Udacity site and elsewhere?

Write Your First Python Application

We have successfully set up and installed a LAMP server on our Ubuntu machine, optimized our server, and now it’s time to write our first Python application. We won’t be writing anything too fancy, but we will cover the basics of a Python application, and also talk a bit to either our MySQL or PostgreSQL database.

Throughout this tutorial, I will be using the nano editor in the terminal to write my Python code, but feel free to use whichever editor you please. It should not affect the outcome of this tutorial.

Python uses whitespaces. via Udacity

Also note that unlike many popular programming languages, Python uses whitespace indentation as a delimiter for code blocks, instead of the traditional curly braces or keywords. This allows Python to have extremely readable code, and forces you not to write single-line functions. Keep this in mind as you are writing your own program, as you don’t want to have issues caused by missing line breaks or indentation.

Creating Our First .py File

Navigate to a folder that you would like to create your first Python file in, and create a file called test.py. Now open this file up in the editor and type the following code:

print "Hello, World!"

Save your file, and in the terminal, navigate to wherever you created this file, and run it by calling python test.py. You should see the correct output:

This is a very simple way to write a Python application and see the result. However, what if we want to run the application without typing python test.py every single time? To do that, we can add the following to our code:

#!/usr/bin/python
print "Hello, World!"

That top line allows the file to locate the Python interpreter (which, by default, is in the /usr/bin/ folder). Save the file. Now we need to change the permission of this file so that it is executable by the terminal. Run the following:

chmod +x test.py

Now we can run the program by simply typing:

./test.py

Now, let’s add a variable to our program, as well as a conditional statement. What we want the variable to do is hold our first name (you’ll want to change the name used in this example to your own), and then our conditional statement will check to see whether the name is correct. If it is, the program will say hello. If it isn’t, the program will ask for the correct name.

#!/usr/bin/python

name = "Keenan"

if name == "Keenan":  
  print ("Hello,", name)
else:  
  print "Oh, well what is your name then?"

Let’s run our program and see the output.

You should see something similar to the above. Now let’s edit our program and misspell the name to see whether our condition is really working:

name = "Keenann"

Run the program again, and you should see:

Now we know that the conditional statement is checking the name, and if it’s different from the expected value, the program will ask for the correct name.

Writing a Simple Function

Let’s take our basic conditional statement and turn it into a function. We’ll write our function with a single argument that will be used to store our name. The program will then ask our user whether the name is correct, and the user can answer yes or no (inside of the terminal). Depending on whether the name is correct or incorrect, the program will display a different message.

#!/usr/bin/python

  def checkName(name):  
    checkName = input("Is your name " + name + "? ") 
    
    if checkName.lower() == "yes":    
      print("Hello,", name)  
    else:    
      print("We're sorry about that.")

checkName("Keenan")

You can define your function by using the def keyword, followed by a name for your function (in this example, it’s called “checkName”). Inside of our function, we declare a single argument, which allows us to input information into the function to be used. After that, we ask our user whether the name is correct, which can be accomplished by using the input method in Python.

Next, we write a conditional similar to the one above, checking to see whether the name entered is correct. If it is, we welcome the user. If it’s not, we apologize. The first line of the conditional has the biggest change, and that’s because we are checking what the user has typed in (via the checkName object) to see whether it was yes or no. We also force the user’s input to be lowercase, so that no matter how they type in “yes” (whether it’s yes, YES, yEs, or Yes), it will always be compared the same way.

At the end of our file, we are simply running the function we just created, and passing in our name as the parameter.

We should now be able to run our script and walk through both conditionals:

Let’s take our function a step further and allow our somebody to enter their name if it’s not the one the program expected. This will only take one additional line and one changed line.

#!/usr/bin/python3

def checkName(name):
  checkName = input("Is your name " + name + "? ") 
  
  if checkName.lower() == "yes":    
    print("Hello,", name)  
  else:    
    name = input("We're sorry about that. What is your name again? ")    
    print("Welcome,", name)

checkName("Keenan")

All of our changes lie right after our else: statement. Instead of just apologizing to the user, we assign a new value to our name parameter, which will both apologize and capture the new name for our user. Afterwards, we welcome our new user.

Running the application should look something like:

Creating MySQL Tables and Accessing Them

Now that we’ve written a simple function and used variables, user input, and conditional statements, let’s connect to our MySQL database (installed previously) to store and retrieve information.

Before we start writing any code, we need to ensure that a MySQL driver is installed on our machine. We’ll do this by running the following command:

sudo apt-get install python-mysqldb

However, before we add any Python code to actually interact with MySQL, we need to create a database table. Let’s run MySQL in our terminal (you will have to enter your root MySQL password afterward):

mysql -u root -p

Now let’s create a database in MySQL named “python”:

CREATE DATABASE python;

Let’s ensure that we’re connected to our new database in the terminal:

connect python

Now let’s create a table called “users” with one field for an auto-incrementing ID and one field for a first name:

CREATE TABLE users (
  id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
  firstname VARCHAR(30) NOT NULL
);

Your terminal should look similar to this:

mysql-create-tables.png

Now that we have our database and tables, let’s set up our Python script so we can access and modify it.

Opening up test.py, we should add the following code to our file:

#!/usr/bin/python
import MySQLdb

# Setup MySQL Connection
db = MySQLdb.connect(host="localhost", user="root", passwd="YOURPASSWORD", db="python")
cursor = db.cursor()

# Insert a row into our table
cursor.execute("INSERT INTO users (firstname) VALUES ('Keenan')")

# Save changes to database
db.commit()

You can see that after we declare our Python path, we import the Python-MySQLdb tool that we installed earlier. This is necessary so our program can interact with our MySQL database. Our next line is creating an object named db that stores our connection to MySQL. Ensure that each field here uses your correct host, username, password, and database (although the only thing that should be changing on your end is the password).

Next, we create an object named cursor that allows us to actually execute queries in MySQL. After that, we use the cursor object we just created and run a MySQL command to insert a row into the users field. In this case, I’m inserting my first name into the firstname field. We then call db.commit() to ensure that all changes we make to our database are saved.

Now if we save our file and run the following command inside of MySQL in our terminal, we should see:

connect python
SELECT * FROM users;

This means that we have successfully inserted a row into our users table in our database. We now know that our Python application is talking to MySQL.

Creating PostgreSQL Tables and Accessing Them

If we want to interact with a PostgreSQL (abbreviated throughout the article as “Postgres”) database, we will need to install and use a tool call psycopg2. We can do that by typing the following into the terminal:

sudo apt-get install python-psycopg2

We need to run Postgres inside of the terminal so that we can create and access our tables, but we need to create a Postgres user first. We’ll do that by first logging in as the default Postgres user:

sudo su - postgres

Now let’s create a Postgres database named “python”:

createdb python

Let’s ensure that we’re connected to our new database in the terminal:

psql -d python

We now need to create a new user and add them to the database so we can execute commands:

CREATE USER keenan WITH PASSWORD 'password';GRANT ALL PRIVILEGES ON DATABASE "python" to keenan;

Now let’s create a table called “users” with one field for an auto-incrementing ID and one field for a first name:

CREATE TABLE "users" ( 
  id SERIAL PRIMARY KEY, 
  firstname VARCHAR(30) NOT NULL 
);

Your terminal should look similar to this:

Now that we have our database and tables, let’s set up our Python script so we can access and modify it.

Opening up test.py, we should add the following code to our file:

#!/usr/bin/python 
import psycopg2  

# Setup PostgreSQL Connection 
db = psycopg2.connect("host=localhost dbname=python user=keenan password=YOURPASSWORD")
cursor = db.cursor()

# Insert a row into our table 
cursor.execute("INSERT INTO users (firstname) VALUES ('Keenan')") # Save changes to database db.commit()

You can see that after we declare our Python path, we import the Psycopg2 tool that we installed earlier. This is necessary so our program can interact with our Postgres database. Our next line is creating an object named db that stores our connection to Postgres. Ensure that each field here uses your correct host, username, password, and database (although the only thing that should be changing on your end is the password).

Next, we create an object named cursor that allows us to actually execute queries in Postgres. After that, we use the cursor object we just created and run a Postgres command to insert a row into the users field. In this case, I’m inserting my first name into the firstname field. We then call db.commit() to ensure that all changes we make to our database are saved.

Now if we save our file and run the following command inside of Postgres in our terminal, we should see:

su - postgres
psql python << EOF

SELECT * FROM users;
EOF

This means that we have successfully inserted a row into our users table in our database. We now know that our Python application is talking to PostgreSQL.

The Finish Line

Congratulations, you now have a very simple Python application that is communicating with your database, storing variables, getting user input, and running conditional statements. You could further expand your program by asking the user their name and storing that in the database, and possibly adding more complexity to your database.