Python Programming - Getting Started and Gitlab - Getting Started: Difference between pages

From Computer Science at Indiana State University
(Difference between pages)
Jump to navigation Jump to search
 
 
Line 1: Line 1:
''This page is part of [[Programming and CS - Getting Started]]''
''This page is part of [[Programming and CS - Getting Started]]''


For a video explaining how to get started here, see https://www.youtube.com/watch?v=uLnhcCZS4-Y&t=424s
=Git=
[https://en.wikipedia.org/wiki/Git Git] is a version control system that can be used to keep track of your files (programming code, but also other files as well). 


==Getting Started==
Two of the most popular installations of git (as of 2020) are [https://github.com/ github] and [https://gitlab.com/ gitlab]. When Microsoft purchased github, it became fashionable to prefer gitlab.  Github allows for projects to be hosted on their servers. Gitlab allows for hosting either on their servers or to run a local installation of gitlab on ones own servers.
# '''Reading''' - start reading through at least one of the following before you start working on the programming problems.
## [https://www.w3schools.com/python/python_intro.asp w3schools Python] - interactive tutorial where you can try out code in the browser
## [https://www.learnpython.org/ Learn Python] - interactive tutorial where you can try out code in the browser
## [https://automatetheboringstuff.com/ Automate the Boring Stuff with Python] - suitable for people with very limited programming experience, this is the text that is being followed for our CS 151 course (as of 2019)
## [http://greenteapress.com/thinkpython2/html/index.html Think Python] - suitable for people with very limited programming experience, not very deep
## [https://learnxinyminutes.com/docs/python3/ LearnXinYminutes] - quick review once you are familiar with the basics
## [https://docs.python.org/3/tutorial/ Python.org tutorial] - good for those with programming experience already
# '''Get Python installed on your computer''' - download the latest Python3 version at https://www.python.org/downloads/.  Having python3 installed on your computer allows you to debug much quicker.
# '''Work on solving problems''' that are listed below.
# If you are a current or incoming ISU student, or an ISU alumni, get help on what you are working on using the online unix lab.  When asking about the hackerrank problems make sure to refer to them using the title hackerrank gives them, or give a link to the problem statement on hackerrank.
# '''Cheat sheets''' - keep a cheat sheet for yourself of python syntax, built-in functions, etc.  See below on this page for our model cheat sheets.


==Running Python==
Of late, we have hosted projects using gitlab either on gitlab's servers or on ISU's local installation of gitlab.
''See [https://youtu.be/ABmQ2QyYCLg this video] for a video demo of getting started running Python on the CS server and in IDLE.''


Once you have a computer with Python installed you are ready to try it out! You can use the CS systems (computers in our classrooms and labs, or connect to the CS server - see [[Linux - System Setup]]), or install Python on your personal computer. ''We recommend doing both.''
=Account Setup=
'''On gitlab.com''' - Account setup on [https://gitlab.com/ gitlab.com] is just like any other website.  You create a free account, choose your username/password, etc.  The account is yours and is not tied in any particular way to ISU. You can give access to your projects in much the same way as you give access to files on google drive, onedrive, dropbox, etc.


===Run in the Terminal===
'''On git.indstate.edu''' - ISU's local installation of gitlab is at https://git.indstate.edu/.  For this system you use your ISU portal id and password.  You do not need to create an account.  The first time you login into the system, it initializes an account for you.  CS courses and ISU CS projects in general tend to use ISU's local installation because we can tie access based on course rosters, etc.
On the CS server, other Linux systems, and macOS, you can run python using your terminal. If you run the command <code>python3</code> then you will see the python console.  Here you can type one python line at a time to run, and the results are displayed.  You can try these -
<pre>
2 + 3
3 * 4
10 ** 3
</pre>
 
We normally store our python code in text files and want to run the entire file.  If you have a file with python code (let's say it is named myProgram.py) then you change directories to the directory that has your file, and then type <code>python3 myProgram.py</code>.  You can have one terminal open where you are running your program with this command, and you can have another terminal open to edit the file (or use some other text editor).


===Run in IDLE===  
=SSH Keys=
Python comes with a graphical front-end called IDLE.  You can run this program if you are in the CS labs by opening your terminal and then typing <code>idle3</code>. If you have python installed on your personal computer, then you can find IDLE in your list of programs (Start button on Windows, Finder then click Applications on macOS) or run idle3 from the terminal (if you are using Linux). If you are connecting to the CS server with a terminal program from home, you cannot run idle3 in the terminal because it is a graphical program - you will need to install it on your home computer to use it from home.
You will normally want to have SSH keys setup so that you can interact with the git server without having to enter a username and password. Instructions are here: https://docs.gitlab.com/user/ssh/


When you start IDLE, it shows you the Python3 console, where you can type your Python3 commands.  Try typing in arithmetic expressions to make sure it is working. For example -
You will need to edit your ssh configuration so that it knows which key to use when connecting to git.indstate.edu. On a Mac or Linux, edit the file <code>~/.ssh/config</code> and put a section like this:
<pre>
<pre>
2 + 3
Host git.indstate.edu
3 * 4
  PreferredAuthentications publickey
10 ** 3
  IdentityFile ~/.ssh/git_indstate_id
</pre>
</pre>
You should replace the name git_indstate_id with whatever filename you used for the private key. On Windows, the file should be in your home directory (normally C:\Users\YOUR_USERNAME) and then .ssh/config (so, similar to where it is for Mac and Linux).
Note - for editing the config file, you should use a plain text editor (Notepad on Windows, TextEdit on Mac, or something nicer; not Word).


===First Python Program===
Note - you will need to enable the ability to see "hidden" files. On Mac, you press cmd-shift-. to see hidden files in Finder. On Windows, in File Explorer you need to: click the triple dots near the top right, click Options, click View, select "Show files, folders, and drives", and also unselect "Hide extensions for known file types".
We store python programs in text files.  You can use the following as your first python program.
<pre>
print('Hello world.')
print('Hello again.')
print('Goodbye now.')
</pre>
If you are using IDLE you create and run a file by -
# Click on File and then New File. 
# Copy/paste or type your code in the file window that comes up.
# Click File and Save As.  Name the file myProgram.py and save it in whichever directory you want to keep your python programs. 
# Click Run and then Run Module.  In the python console window you should see that your program ran.
If you are connecting to the CS server with your terminal from your home computer, you can choose one of the following options.
* Create and edit the myProgram.py file with a text editor that works in the terminal - see [[Linux Terminal - Text Editors]]. Save the program and run the program with <code>python3 myProgram.py</code>.  If you make changes, edit the program, and then run it again.
* Edit the program using a text editor on your home computer (for example, Kate or Atom, see [[Linux Terminal - Text Editors]]), transfer to the CS server using a file transfer program (see [[Linux - System Setup]]), change directories in your terminal to the directory that has the program, and then run the program in the terminal with <code>python3 myProgram.py</code>


===Python2 versus Python3===
You can clone a repository from git.indstate.edu using a command like this: <code>git clone git+ssh://git@git.indstate.edu/ect-437-fall-2026/hello-class.git</code>, but where you would replace the link with whatever repository you are cloning.
If you want to use Python version 2 instead, you would use commands <code>python</code> and <code>idle</code> rather than <code>python3</code> and <code>idle3</code>.  You should normally use Python3; the only reason to use Python2 is if you are using a package that is only available on Python2 (Python2 is not officially supported anymore, so this should be fairly rare).


==Installing Python on Your Personal Computer==
=Getting Around=
'''Windows and macOS''' Download and install from https://www.python.org/downloads/.   
Whether you are using gitlab.com or git.indstate.edu, the functionality and "how to get around" is about the same.  You have two main methods for using either.


'''Linux''' On Linux python is probably already installed. If not do an internet search for your Linux distribution and "install python3" (i.e., search for - ubuntu install python).
==Web browser interface==
You can view projects within your web browser.  How to get around is fairly intuitive - as is the case with any web page, you look around and click around to see what different things do. When you have something specific you want to do but don't see how to do, you ask the internet or ask your instructor (and we may update this wiki to give instructions on how to do it).


'''Chromebook''' You should first get the Linux development environment enabled (do an internet search for directions) and then you can follow instructions for installing python3 for ubuntu.  You will need to enter commands into the Terminal application. You open the Terminal application and then click on the link under "Linux" in the application (for me it says penguin, but may say something different for you). If python3 is not already installed, then running the following should do install it.
==Terminal interface==
<pre>
Git allows for downloading/uploading/etc. files in your projects from the terminal.  In order to do this, you need to (a) have a git client installed on your system, (b) understand the terminology that git uses for keeping track of files and the steps in using git to download/update/upload files, (c) make it so.
sudo apt-get update
sudo apt-get install python3
sudo apt-get install idle
</pre>
Once those finish, you start idle by opening the terminal and then typing the idle command. Note that if you download any files with your browser, in order to use them in IDLE (or other programs that you run from the terminal) you need to copy them to the Linux files folder.


==Lists of Problems==
Some reading material about using git - https://www.tutorialspoint.com/git/index.htm
===Programming Assignments 1===
[[Programming Assignments - Beginning 1]] - start with trying to solve these problems.  Each requires a different feature of the Python programming language, so solve these problems as you read through one of the tutorials or links above. Note that the page includes a link to repl.it that contains solutions to most of the problems.  If you do not have Python installed on your computer, you can try it out at [https://repl.it/ repl.it] - click the logo at the top, then click "+ new repl", select Python and Create Repl.


[[Programming Assignments - Beginning 2]] - another set of classic beginning programming exercises.  Some of these will be more involved.
===Vocab / Cheat Sheet===


===HackerRank Problems===
===Workflow===
Once you can do some basic Python programming it is time to have some of your work checked.  Hackerrank is a site where you can create an account and work on problems that will be checked if they are correct. Note that hackerrank has very strict rules for accepting correct solutions.  Start with the basic problems to get a feel for what hackerrank expects.
A common use case is that you will need to download someone else's (e.g., instructor's) gitlab project, create your own version of some part of the project, test/debug your version of the project, eventually put in a request to update the main project with your code.  The steps involved in that workflow.


[[Python programming - hackerrank problems]]
'''Clone''' - To download the current version of the project you (a) browse in your terminal to the directory where you want to have the project code, (b) run <code>git clone url_to_project</code>


===More Practice===
'''Branch''' - If a branch has already been created for you, then you can set that as your active branch by running <code>git checkout branch_name</code>.  If a branch has not yet been created for you, you run <code>git branch branch_name</code> to create a new branch. In either case, you have a branch of the project to work within.
If you are able to do most of the problems on the pages linked above, then you don't need us to give you lists of problems any more.  You can pick problems to work through on your own.  Some suggested places with problems are as follows.
* [https://www.hackerrank.com/domains/algorithms Hackerrank - algorithms]
* [https://www.hackerrank.com/domains/data-structures Hackerrank - data structures]
* [http://cs.indstate.edu/acm/contests.html ACM Contest Problems]


==Cheat Sheets and Quizzes==
'''Do your work''' - You work on your files locally - test, debug, etc.
These cheat sheets have the highlights of some of the basic information to memorize when you are in your first year of Python programming.  Note - each cheat sheet contains a sample quiz that might be used by your instructor.
* [[Cheat sheet - Python Operators, Expressions]]
* [[Cheat sheet - Python Keywords, Concepts, Functions]]


==Programming Errors and How to Fix Them==
'''Stage/Commit/Push''' - To save your work to the remote server, you need to stage, commit, and push.  You can stage/commit batches of changes, or do a stage/commit for each discreet task accomplished.  If you are working in some_file.txt, you would run the following sequence of commands.
* '''Syntax error''' - program is not a valid program, and normally python gives you an error message giving some indication of what the problem was. 
** ''Debugging'' - read the error message!  Normally you are given a line number where python got "confused" - sometimes that is where the problem is, but you may need to look just before that line (or just after).  Common syntax errors in python...
** ''Mismatched'' () or [] or "" or '', for example <code>x = 2 * (3 + 4</code>
** ''Mis-capitalization or mis-spelling'', for example <code>Print('hello')</code>
** ''Improper indenting'', for example
** ''Data type'' problems - mixing up strings and numbers, for example <code>x = '3' + 4</code>
<pre>
<pre>
for i in range(0, 10):
git add some_file.txt  # this "stages" the file to be part of the next commit
print(i)
git commit -m "Some updates to the file" # This commits, will be a permanent record of this version of the file
git push              # sends all committed changes to the remote server
</pre>
</pre>
* '''Runtime error''' - program is a valid program but when you run it something goes wrong.  Debugging is similar as for syntax errors and logical errors.  For example...
Note that the push may ask for login information to the remote server.  If you have an ssh key configured for the project (see section above), then you won't be asked for login information.
<pre>
 
x = 1
'''Pull''' - To pull/download updates that have happened (by others), you use <code>git pull</code>.
print(x)
 
x = y + z
'''Merge''' - When you ready to have your changes merged in to the main branch of the project, ask the owner of the project to check your branch.  When the owner is ready for it, the owner can run the following commands.
</pre>
* '''Logical error''' - program runs but does not do the right thing.
** ''Print debugging'' - One debugging method is to start printing things (values of variables) in your program starting at the beginning, run the program, make sure it is printing the values you expect, at some point you see a value you don't expect, and that tells you where to look in your code.  You often are 100% sure what you have is right, but in the end there is some problem.  So you have to ''check everything'', start at the very beginning of the program. 
** ''Disable all'' - Another strategy is to disable almost all parts of your program (e.g., put it all in comments) until you get behavior that makes sense, and then start adding things back in (uncommenting), and eventually you'll add something in that doesn't behave the way you thought, and now you know where the problem is.  Examples...
<pre>
<pre>
x = 1
git branch main        # owner is working in the main branch
y = 2
git merge branch_name  # merge your branch into the main branch
z = 3
avg = x + y + z / 3
print(avg)
</pre>
</pre>

Revision as of 20:15, 23 August 2026

This page is part of Programming and CS - Getting Started

Git

Git is a version control system that can be used to keep track of your files (programming code, but also other files as well).

Two of the most popular installations of git (as of 2020) are github and gitlab. When Microsoft purchased github, it became fashionable to prefer gitlab. Github allows for projects to be hosted on their servers. Gitlab allows for hosting either on their servers or to run a local installation of gitlab on ones own servers.

Of late, we have hosted projects using gitlab either on gitlab's servers or on ISU's local installation of gitlab.

Account Setup

On gitlab.com - Account setup on gitlab.com is just like any other website. You create a free account, choose your username/password, etc. The account is yours and is not tied in any particular way to ISU. You can give access to your projects in much the same way as you give access to files on google drive, onedrive, dropbox, etc.

On git.indstate.edu - ISU's local installation of gitlab is at https://git.indstate.edu/. For this system you use your ISU portal id and password. You do not need to create an account. The first time you login into the system, it initializes an account for you. CS courses and ISU CS projects in general tend to use ISU's local installation because we can tie access based on course rosters, etc.

SSH Keys

You will normally want to have SSH keys setup so that you can interact with the git server without having to enter a username and password. Instructions are here: https://docs.gitlab.com/user/ssh/

You will need to edit your ssh configuration so that it knows which key to use when connecting to git.indstate.edu. On a Mac or Linux, edit the file ~/.ssh/config and put a section like this:

Host git.indstate.edu
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/git_indstate_id

You should replace the name git_indstate_id with whatever filename you used for the private key. On Windows, the file should be in your home directory (normally C:\Users\YOUR_USERNAME) and then .ssh/config (so, similar to where it is for Mac and Linux).

Note - for editing the config file, you should use a plain text editor (Notepad on Windows, TextEdit on Mac, or something nicer; not Word).

Note - you will need to enable the ability to see "hidden" files. On Mac, you press cmd-shift-. to see hidden files in Finder. On Windows, in File Explorer you need to: click the triple dots near the top right, click Options, click View, select "Show files, folders, and drives", and also unselect "Hide extensions for known file types".

You can clone a repository from git.indstate.edu using a command like this: git clone git+ssh://git@git.indstate.edu/ect-437-fall-2026/hello-class.git, but where you would replace the link with whatever repository you are cloning.

Getting Around

Whether you are using gitlab.com or git.indstate.edu, the functionality and "how to get around" is about the same. You have two main methods for using either.

Web browser interface

You can view projects within your web browser. How to get around is fairly intuitive - as is the case with any web page, you look around and click around to see what different things do. When you have something specific you want to do but don't see how to do, you ask the internet or ask your instructor (and we may update this wiki to give instructions on how to do it).

Terminal interface

Git allows for downloading/uploading/etc. files in your projects from the terminal. In order to do this, you need to (a) have a git client installed on your system, (b) understand the terminology that git uses for keeping track of files and the steps in using git to download/update/upload files, (c) make it so.

Some reading material about using git - https://www.tutorialspoint.com/git/index.htm

Vocab / Cheat Sheet

Workflow

A common use case is that you will need to download someone else's (e.g., instructor's) gitlab project, create your own version of some part of the project, test/debug your version of the project, eventually put in a request to update the main project with your code. The steps involved in that workflow.

Clone - To download the current version of the project you (a) browse in your terminal to the directory where you want to have the project code, (b) run git clone url_to_project

Branch - If a branch has already been created for you, then you can set that as your active branch by running git checkout branch_name. If a branch has not yet been created for you, you run git branch branch_name to create a new branch. In either case, you have a branch of the project to work within.

Do your work - You work on your files locally - test, debug, etc.

Stage/Commit/Push - To save your work to the remote server, you need to stage, commit, and push. You can stage/commit batches of changes, or do a stage/commit for each discreet task accomplished. If you are working in some_file.txt, you would run the following sequence of commands.

git add some_file.txt  # this "stages" the file to be part of the next commit
git commit -m "Some updates to the file" # This commits, will be a permanent record of this version of the file
git push               # sends all committed changes to the remote server

Note that the push may ask for login information to the remote server. If you have an ssh key configured for the project (see section above), then you won't be asked for login information.

Pull - To pull/download updates that have happened (by others), you use git pull.

Merge - When you ready to have your changes merged in to the main branch of the project, ask the owner of the project to check your branch. When the owner is ready for it, the owner can run the following commands.

git branch main        # owner is working in the main branch
git merge branch_name  # merge your branch into the main branch