Difference between revisions of "Python Programming - Getting Started"

From Computer Science
Jump to: navigation, search
(Running Python on ISU CS Systems)
Line 15: Line 15:
  
 
==Running Python==
 
==Running Python==
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.
+
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.''
  
'''Python Console''' 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.   
+
===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 displayed.   
  
'''Python Files''' A python program is a text file with python commands. You can use any text editor to edit the file.  You should save your python files so they end in ".py".  You run a python program by either using the terminal or the python graphical front-end.
+
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 the Terminal''' On the CS server, other Linux systems, and macOS, you can run a python program using your terminal.  You change directories to the directory that has your python 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===
 +
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 because it is a graphical program - you will need to install it on your home computer to use it.
  
'''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 <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).
+
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 -  
 
 
'''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 (the Sage math package as of 2019 was only available for Python2).
 
 
 
==Running Python on Your Personal Computer==
 
'''Install''' First make sure Python3 is installed - download from https://www.python.org/downloads/.  Note that if you are using Linux then python3 is probably already installed; if not do an internet search for your Linux distribution and "install python3" (i.e., ubuntu install python).  If you are using a Chromebook, then 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.
 
 
 
'''Open IDLE''' Once installed, you will run the program called IDLE, which is the graphical front-end for Python3.  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 -  
 
 
<pre>
 
<pre>
 
2 + 3
 
2 + 3
Line 37: Line 32:
 
</pre>
 
</pre>
  
'''Create a Python file''' We store python programs in text files.   
+
===First Python Program===
# To create your first Python program file in IDLE, click on File and then New File. 
+
We store python programs in text files.  You can use the following as your first python program.
# Type <code>print('Hello World!')</code> in the file
 
# Click File and Save As.  Name the file hello.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.
 
# In your hello.py program, change it so that the code is the following -
 
 
<pre>
 
<pre>
 
print('Hello world.')
 
print('Hello world.')
Line 48: Line 39:
 
print('Goodbye now.')
 
print('Goodbye now.')
 
</pre>
 
</pre>
# Click Run and then Run Module to run the file again.  In the console you should see that the program ran again.
+
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.
 +
* Edit the myProgram.py 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 the 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 (the Sage math package as of 2019 was only available for Python2).
 +
 
  
'''That's it'''  Now you are ready to learn more python from the sources linked on this pageYou will put your code into python files, which are text files that end in ".py". You run the files inside of
+
==Installing Python on Your Personal Computer==
 +
'''Install''' Download from https://www.python.org/downloads/. Note that if you are using Linux then python3 is probably already installed; if not do an internet search for your Linux distribution and "install python3" (i.e., ubuntu install python)If you are using a Chromebook, then 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.
  
 
==Lists of Problems==
 
==Lists of Problems==

Revision as of 16:14, 16 January 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. Learn Python - interactive tutorial where you can try out code in the browser
    2. 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)
    3. Think Python - suitable for people with very limited programming experience, not very deep
    4. LearnXinYminutes - quick review once you are familiar with the basics
    5. 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. 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

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.

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 because it is a graphical program - you will need to install it on your home computer to use it.

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.

  • Edit the myProgram.py 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 the 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 (the Sage math package as of 2019 was only available for Python2).


Installing Python on Your Personal Computer

Install Download from https://www.python.org/downloads/. Note that if you are using Linux then python3 is probably already installed; if not do an internet search for your Linux distribution and "install python3" (i.e., ubuntu install python). If you are using a Chromebook, then you should first get Linux (beta) installed (see [[1]]) and then you can follow instructions for installing python3 for ubuntu.

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

These cheat sheets have the highlights of some of the basic information to memorize when you are in your first year of Python programming.