{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 1. Introduction\n",
"\n",
"\n",
"- HTML version of textbook: [http://openbookproject.net/thinkcs/python/english3e/way_of_the_program.html](http://openbookproject.net/thinkcs/python/english3e/way_of_the_program.html)\n",
"- PDF version of textbook: [http://www.greenteapress.com/thinkpython/thinkCSpy/thinkCSpy.pdf](http://www.greenteapress.com/thinkpython/thinkCSpy/thinkCSpy.pdf)\n",
"\n",
"## Topics:\n",
"- Python and its features\n",
"- Ways to run Python code\n",
"- Computer program and its building blocks\n",
"- Errors and Debugging\n",
"\n",
"## 1.1 Python Programming Language\n",
"\n",
"- the single most important skill for a computer scientist is problem solving\n",
"- Python is a tool that helps computer scientists and programmers solve problems by writing code\n",
"- One of the most popular programming languages used in various fields: Data Science and Machine Learning, Security, Web Apps, etc.\n",
"- Popularity has been increasing over the years: https://www.tiobe.com/tiobe-index/"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1.2 Python Features\n",
"- high-level general purpose programming language such as PHP, Perl, Java, JavaScript, C++, etc.\n",
" - as opposed to low level machine language such as assembly\n",
"- interpreted language; needs Python interpreter to execute Python code\n",
"- platform independent/portable; python programs can be run in many platforms including Raspberry Pi\n",
"- open source, can be freely downloaded and use: http://python.org\n",
"- installed using Python package manager such as Anaconda or Miniconda: https://www.anaconda.com/download/\n",
"- two versions Python 2.x and Python 3.x - Notebooks and the text use version 3.x which is the new standard"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1.3 Zen of Python"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The Zen of Python, by Tim Peters\n",
"\n",
"Beautiful is better than ugly.\n",
"Explicit is better than implicit.\n",
"Simple is better than complex.\n",
"Complex is better than complicated.\n",
"Flat is better than nested.\n",
"Sparse is better than dense.\n",
"Readability counts.\n",
"Special cases aren't special enough to break the rules.\n",
"Although practicality beats purity.\n",
"Errors should never pass silently.\n",
"Unless explicitly silenced.\n",
"In the face of ambiguity, refuse the temptation to guess.\n",
"There should be one-- and preferably only one --obvious way to do it.\n",
"Although that way may not be obvious at first unless you're Dutch.\n",
"Now is better than never.\n",
"Although never is often better than *right* now.\n",
"If the implementation is hard to explain, it's a bad idea.\n",
"If the implementation is easy to explain, it may be a good idea.\n",
"Namespaces are one honking great idea -- let's do more of those!\n"
]
}
],
"source": [
"import this"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1.4 Learning and Writing Python Code\n",
"- one must write code to learn it\n",
"- Python provides Chevron prompt to practice and use Python for quick calculations such as a calculator\n",
"- most use code editors to write long program also called scripts\n",
"- use [pythontutor.com](http://www.pythontutor.com/) to visualize code execution\n",
"\n",
"### Chevron Prompt\n",
"- Python provides a prompt in terminal - interactive mode\n",
"- Once Python is installed and configured correctly, open terminal and type Python\n",
"- [python.org](https://www.python.org/) also provides online Python prompt\n",
"- \\>>> You'll see this chevron/python prompt\n",
"- \\>>> 10 + 20.5\n",
"- \\>>> print('Hello World!')\n",
"\n",
"### Jupyter Notebook\n",
"- interactive notebook that can have live code, execution results and HTML, texts and multimedia contents!\n",
"- great way to learn, experiment, and take notes while coding\n",
"- Jupyter supports many programming languages such as C, C++, Java, JavaScript, etc.; Python is default!\n",
"\n",
"### Python Script \n",
"- using Intergrated Development Environment (IDE) such as PyScripter, PyCharm, and Visual Studio Code, Nodepad++, etc.\n",
"- open VS Code or your favourite editor\n",
"- create a hello.py file\n",
"- type print('hello world!') and save the file\n",
"- run the program from integreted terminal:\n",
"```bash\n",
"python hello.py\n",
"```\n",
"- run the program using GUI Run button\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1.5 Computer Program\n",
"- sequence of instructions that specifies how to perform a computation\n",
"- some basic/fundamental concepts that make up a compupter program:\n",
" - input, output, math, conditional execution and repition\n",
"\n",
"\n",
"### input\n",
"- get data from keyboard, a file, or some device\n",
"\n",
"### output\n",
"- display data/answer on screen, or save it to file or to a device\n",
"\n",
"### math\n",
"- basic mathematical operations such as addition, subtraction, multiplication, etc.\n",
"\n",
"### conditional execution\n",
"- check for certain condititions and execute appropriate sequence of statements\n",
"\n",
"### repitition\n",
"- perform some action repeatedly, usually with some variation every time"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1.6 Debugging\n",
" - finding and getting rid of bugs/errors\n",
" - as long as humans write computer codes, there'll be always errors in computer program\n",
" - although frustrating at times, it is one of the most intellectually rich, challenging, and interesting part of programming"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1.7 Coding Errors\n",
"- errors are also called bugs\n",
"- 3 types: syntax, run-time and semantic\n",
"\n",
"### Syntax errors\n",
"- program needs to follow Python syntax or grammar; otherwise Python interpreter will not understand and tell programmers about the errors"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Run-time errors\n",
"- also called run-time exceptions\n",
"- errors appear while programming is running\n",
"- can be handled to certain extent\n",
"\n",
"### Semantic errors\n",
"- program runs fine but gives wrong answer\n",
"- can be identified and removed by doing plenty of testing"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" ## 1.8 The First Program \n",
" - python programs are usually called scripts"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hello world!\n"
]
}
],
"source": [
"#----------------------------------------------------------\n",
"# hello world program\n",
"# by: John Doe\n",
"# Jan 1 2017\n",
"# Copyright: Anyone may freely copy or modify this program\n",
"#----------------------------------------------------------\n",
"print('hello world!') # say hello to the beautiful world!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1.9 Exercises\n",
"\n",
"### 1. Write a hello world script\n",
"- write a python script that prints \"Hello World!\" as an output on the console\n",
"\n",
"### 2. Solve kattis hello problem\n",
"- create an account in Kattis: https://open.kattis.com/login\n",
"- login and solve the hello problem: https://open.kattis.com/problems/hello\n",
"\n",
"### 3. ASCII Art\n",
"- learn about ASCII Art\n",
"- print some ASCII Arts, texts and pictures of your choice\n",
"- use ASCII Art generator: http://patorjk.com/software/taag/#p=display&f=Graffiti&t=Type%20Something%20\n",
"\n",
"### 4. Write a script that prints various stages of hangman game\n",
"- game description: https://en.wikipedia.org/wiki/Hangman_(game)\n",
"- produce the output seen in Example game section of the Wikipedia page\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}