Difference between revisions of "Python Programming - Getting Started"

From Computer Science
Jump to: navigation, search
(Getting Started)
(Getting Started)
 
(47 intermediate revisions by the same user not shown)
Line 1: Line 1:
''This bootcamp is part of the [[Programming / CS Bootcamps]]''
+
''This page is part of [[Programming and CS - Getting Started]]''
  
For a video explaining how to get started with this bootcamp, see https://www.youtube.com/watch?v=uLnhcCZS4-Y&t=424s
+
For a video explaining how to get started here, see https://www.youtube.com/watch?v=uLnhcCZS4-Y&t=424s
  
 
==Getting Started==
 
==Getting Started==
 
# '''Reading''' - start reading through at least one of the following before you start working on the programming problems.
 
# '''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://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)
 
## [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)
Line 10: Line 11:
 
## [https://learnxinyminutes.com/docs/python3/ LearnXinYminutes] - quick review once you are familiar with the basics
 
## [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
 
## [https://docs.python.org/3/tutorial/ Python.org tutorial] - good for those with programming experience already
# '''Work on solving these problems''' - https://www.hackerrank.com/domains/python.  Start with the first problem and work your way up from there.
 
 
# '''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.
 
# '''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.
# If you are a current or incoming ISU student, or an ISU alumni, '''sign up for the ISU CS Mattermost''' at https://judy.indstate.edu.  Note that you need to use your @sycamores.indstate.edu email address to sign up.  Look for the Python Programming Channel (direct link is https://judy.indstate.edu/isu-cs/channels/python-programming).  When asking about the hackerrank problems make sure to refer to them using the title hackerrank gives them.
+
# '''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.
  
Let's see how far you can get with solving the hackerrank problems!  Good luck!
+
==Running Python==
 +
''See [https://youtu.be/ABmQ2QyYCLg this video] for a video demo of getting started running Python on the CS server and in IDLE.''
  
==Running Python on ISU CS Systems==
+
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.''
If you are using one of the CS lab computers or have a terminal open connected to the CS server, you can run python programs using one of the following methods.
 
  
'''Execute in the terminal''' Edit your python code with a text editor, and run it with the python3 or python commandFor example,
+
===Run in the Terminal===
 +
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 displayedYou can try these -
 
<pre>
 
<pre>
cd ~
+
2 + 3
nano hello.py
+
3 * 4
# and put some python code in hello.py then exit nano
+
10 ** 3
python3 hello.py
 
 
</pre>
 
</pre>
If you want to run the program with Python2, you would use the command <code>python</code> rather than <code>python3</code>.
 
  
'''Idle'''  You can use the builtin Python3 graphical editor to edit your programs and also run them (only if you are on one of the lab computers or tunneling X)The command for Python3 is <code>idle3</code>, and the command for Python3 is <code>idle</code>.
+
We normally store our python code in text files and want to run the entire fileIf 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).
  
==List of Problems==
+
===Run in IDLE===  
Here are problems to work on from the hackerrank set. Note that you should work on more than just this set, but these are ones we are ready to help you solve!
+
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. 
  
===Basic Python===
+
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 -
These problems are all pretty basic - require a single loop, if statement, etc. They are good when you are just getting started with Python.
+
<pre>
* [https://www.hackerrank.com/challenges/py-hello-world/problem Py Hello World]
+
2 + 3
* [https://www.hackerrank.com/challenges/py-if-else/problem Py If Else]
+
3 * 4
* [https://www.hackerrank.com/challenges/python-arithmetic-operators/problem Python Arithmetic Operators]
+
10 ** 3
* [https://www.hackerrank.com/challenges/python-loops/problem Python Loops]
+
</pre>
* [https://www.hackerrank.com/challenges/swap-case/problem Swap Case]
+
 
For a bit more practice with basic problems, try out more of the problems that are listed as "Easy". Once you have completed the once listed in this section you should be ready to complete more of the "Easy" problems on your own.
+
===First Python Program===
* [https://www.hackerrank.com/challenges/write-a-function/problem Write a Function]
+
We store python programs in text files.  You can use the following as your first python program.
* [https://www.hackerrank.com/challenges/text-wrap/problem Text Wrap]
+
<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===
 +
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==
 +
'''Windows and macOS''' Download and install from https://www.python.org/downloads/
 +
 
 +
'''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). 
 +
 
 +
'''Chromebook''' You should first get Linux (beta) installed (see [[https://cs.indstate.edu/wiki/index.php/Linux_-_System_Setup#Chromebook]]) and then you can follow instructions for installing python3 for ubuntu.  If python3 is not already installed, then running <code>sudo apt install python3</code> should install it.
 +
 
 +
==Lists of Problems==
 +
===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.
  
===A Bit More Involved===
+
[[Programming Assignments - Beginning 2]] - another set of classic beginning programming exercises. Some of these will be more involved.
These problems require nested loops, working with lists of lists, reading a problem statement that takes more time to understand, or other things that are the next level of difficulty. Solve all of the Basic Python problems before starting on these.
 
* [https://www.hackerrank.com/challenges/nested-list/problem Nested List]
 
* [https://www.hackerrank.com/challenges/list-comprehensions/problem List Comprehensions]
 
* [https://www.hackerrank.com/challenges/merge-the-tools/problem Merge the Tools]
 
* [https://www.hackerrank.com/challenges/no-idea/problem No Idea]
 
* [https://www.hackerrank.com/challenges/validating-credit-card-number/problem Validating Credit Card Number]
 
* [https://www.hackerrank.com/challenges/words-score/problem Words Score]
 
* [https://www.hackerrank.com/challenges/compress-the-string/problem Compress the String]
 
* [https://www.hackerrank.com/challenges/word-order/problem Word Order]
 
  
===And a Bit More===
+
===HackerRank Problems===
These problems are still a bit more involved. Some require some abstract thinking about the problem, a programming "trick", or other key insight. Note that at this point you are working on problems such that some of the lab assistants in the unix lab may not have solved these problems. This is where you want to be at - you have mastered the basics and are working on problems independently!
+
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.
* [https://www.hackerrank.com/challenges/validating-postalcode/problem Validating Postalcode]
 
* [https://www.hackerrank.com/challenges/matrix-script/problem Matrix Script]
 
* [https://www.hackerrank.com/challenges/maximize-it/problem Maximize It]
 
  
===After That===
+
[[Python programming - hackerrank problems]]
If you are able to do all of the problems 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.
+
 
 +
===More Practice===
 +
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/algorithms Hackerrank - algorithms]
 
* [https://www.hackerrank.com/domains/data-structures Hackerrank - data structures]
 
* [https://www.hackerrank.com/domains/data-structures Hackerrank - data structures]
 
* [http://cs.indstate.edu/acm/contests.html ACM Contest Problems]
 
* [http://cs.indstate.edu/acm/contests.html ACM Contest Problems]
  
==Study Guide==
+
==Cheat Sheets and Quizzes==
In this section we plan to develop a study guide for use in CS 151Check back for a laundry list of concepts, keywords, builtin packages, etc. that you should know.
+
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==
 +
* '''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>
 +
for i in range(0, 10):
 +
print(i)
 +
</pre>
 +
* '''Runtime error''' - program is a valid program but when you run it something goes wrongDebugging is similar as for syntax errors and logical errors.  For example...
 +
<pre>
 +
x = 1
 +
print(x)
 +
x = y + z
 +
</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>
 +
x = 1
 +
y = 2
 +
z = 3
 +
avg = x + y + z / 3
 +
print(avg)
 +
</pre>

Latest revision as of 15:24, 27 July 2020

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

Getting Started

  1. Reading - start reading through at least one of the following before you start working on the programming problems.
    1. w3schools Python - interactive tutorial where you can try out code in the browser
    2. Learn Python - interactive tutorial where you can try out code in the browser
    3. 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)
    4. Think Python - suitable for people with very limited programming experience, not very deep
    5. LearnXinYminutes - quick review once you are familiar with the basics
    6. Python.org tutorial - good for those with programming experience already
  2. 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.
  3. Work on solving problems that are listed below.
  4. 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.
  5. 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

See 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.

Run in the Terminal

On the CS server, other Linux systems, and macOS, you can run python using your terminal. If you run the command python3 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 -

2 + 3
3 * 4
10 ** 3

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 python3 myProgram.py. 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

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 idle3. 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.

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 -

2 + 3
3 * 4
10 ** 3

First Python Program

We store python programs in text files. You can use the following as your first python program.

print('Hello world.')
print('Hello again.')
print('Goodbye now.')

If you are using IDLE you create and run a file by -

  1. Click on File and then New File.
  2. Copy/paste or type your code in the file window that comes up.
  3. Click File and Save As. Name the file myProgram.py and save it in whichever directory you want to keep your python programs.
  4. 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 python3 myProgram.py. 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 python3 myProgram.py

Python2 versus Python3

If you want to use Python version 2 instead, you would use commands python and idle rather than python3 and idle3. 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

Windows and macOS Download and install from https://www.python.org/downloads/.

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).

Chromebook You should first get Linux (beta) installed (see [[1]]) and then you can follow instructions for installing python3 for ubuntu. If python3 is not already installed, then running sudo apt install python3 should install it.

Lists of Problems

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 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.

HackerRank Problems

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.

Python programming - hackerrank problems

More Practice

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.

Cheat Sheets and Quizzes

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.

Programming Errors and How to Fix Them

  • 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 x = 2 * (3 + 4
    • Mis-capitalization or mis-spelling, for example Print('hello')
    • Improper indenting, for example
    • Data type problems - mixing up strings and numbers, for example x = '3' + 4
for i in range(0, 10):
print(i)
  • 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...
x = 1
print(x)
x = y + z
  • 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...
x = 1
y = 2
z = 3
avg = x + y + z / 3
print(avg)