{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# CSCI2040: Introduction to Python\n",
"\n",
"## Administrative matters\n",
"\n",
"**Instructor:** John C.S. Lui, Computer Science & Engineering Department (CSE), CUHK\n",
"\n",
"**Email:** cslui@cse.cuhk.edu\n",
"\n",
"**Course webpage:** www.cse.cuhk.edu.hk/~cslui/csci2040.html\n",
"\n",
"**Lecture:** September 7th, 2020 till October 31, 2020 (*approximately*)\n",
"\n",
"**Comment about final grade:** Note that I don't decide on the final letter grade. The department Examination Panel can always overrule a grade given by the course instructor. So please don't complain to the course instructor!!!\n",
"\n",
"**Prepared on:** August 2nd, 2020.\n",
"\n",
"**Important note:** *If you want to use and modify this notebook file, please acknowledge the author.*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Course outline\n",
"\n",
"- Python, software installation & environmnet\n",
"- Variables and data types\n",
"- Inputs and outputs\n",
"- Variable assignment\n",
"- Control flow: while, if, for\n",
"- Logical operators\n",
"- Lists\n",
"- Tuples\n",
"- Dictionaries\n",
"- List Comprehension\n",
"- Object-oriented programming\n",
"- Functional programming\n",
"- Packages (e.g., matplotlib)\n",
"- Examples\n",
"- ...etc\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Eclipse IDE\n",
"\n",
"- Eclipse is a powerful IDE \n",
"- Download: http://www.eclipse.org/downloads/eclipse-packages/\n",
"- Use Eclipse IDE for Java Developers (use this package)\n",
"- You need to install PyDev in Eclipse\n",
" * http://www.eclipse.org/downloads/eclipse-packages/ (drag to Eclipse)\n",
" * Or https://codeyarns.com/2014/12/23/how-to-install-pydev/\n",
"- Tutorial on PyDev Pluggin in Eclipse\n",
" * Python Development with PyDev and Eclipse – Tutorial:\n",
" -- http://www.vogella.com/tutorials/Python/article.html\n",
" * Python Programming in the Eclipse IDE:\n",
" -- https://www.ics.uci.edu/~pattis/common/handouts/introtopythonineclipse/"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Python: Installation \n",
"\n",
"We will go through how to\n",
"- Install python\n",
"- Different ways to launch python\n",
"- Installation of Jupyter\n",
"\n",
"Prepared by John C.S. Lui, August 2nd, 2020."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Installation of Python\n",
"\n",
"To install Python, do the following:\n",
"\n",
"- Go to https://www.python.org\n",
"- Go to the \"Download\"\n",
" - Go can download 3.X or 2.Y (**Note**: there are some subtle difference between 2.7 and 3.6)\n",
"- For Python documentation, please go to \"Documentation\". You can use both 2.x and/or 3.x documentation. (**Important** !!!!!!)\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Launching Python\n",
"\n",
"Once you have installed python, you can now develop python program. There are at least two ways to launch python:\n",
"- Through terminal:\n",
" - Use your favorite editor to write a python program, say *testing.py*\n",
" - run the python program in your termina:\n",
" - *python testing.py*\n",
" - *python3 testing.py*\n",
" - *python2 testing.py*\n",
" - note that the error in running python or python3 when we don't comment out the last line\n",
" - launch the IDE environment:\n",
" - *idle*\n",
" - *idle3*\n",
" - *idle2*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Demo in class using *terminal* and *idle*\n",
"\n",
"- Show we can use Python as a calculator\n",
"- Show the first 'hello world' Python program in IDLE\n",
"- Show how to run a program in a terminal"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Input and output functions in Python\n",
"\n",
"First, we want to know how to read/write information from the terminal.\n",
"Let's see how we can provide input and output, which are *input()* and *print()*."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hello to the Python world !!\n",
"I hate JohnLui\n",
"I hate John Lui\n",
"I think CSCI2040 is boring. I want to skip this boring class !!!\n"
]
}
],
"source": [
"# Show how we print in Python 3.x\n",
"print(\"hello to the Python world !!\") # this is the first line\n",
"print(\"I hate John\" + \"Lui\")\n",
"print('I hate John', 'Lui')\n",
"print('I think CSCI' + \"2040\" + ' is boring.' + \" I want to skip this boring class !!!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Notes\n",
"\n",
"1. Note how we use single quote and double quote\n",
"2. For concatenation, we use \"+\"\n",
"3. Note about no space between concatenation\n",
"4. Note the automatic space insertion when we use \",\"\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Please enter a name: John C.S. Lui\n",
"hello world !!\n",
"I hate John C.S. Lui , he just has an ugly face !!!!\n",
"I think CSCI2040 is utterly boring.\n",
"Another name: James Bond\n",
"According to Prof. James Bond, the department should not teach Python, it is a bad programming language!!\n"
]
}
],
"source": [
"# Let's ask user for an input\n",
"\n",
"new_name = input(\"Please enter a name: \") # enter a name and assign it to the variable new_name\n",
" # new_name contains a character string\n",
"\n",
"print(\"hello world !!\")\n",
"print(\"I hate \" + new_name, ', he just has an ugly face !!!!')\n",
"print(\"I think CSCI\" + \"2040\" + \" is utterly boring.\")\n",
"\n",
"another_name = input('Another name: ')\n",
"print(\"According to Prof. \" + another_name + \", the department should not teach Python, it is a bad programming language!!\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Please enter a name: John C.S. Lui\n",
"Enter the age of the person above: 21\n",
"hello world !!\n",
"I hate John C.S. Lui, I believe he is not 21 years old.\n",
"BTW, I think CSCI2040 is REALLY amazing due to 'dynamic typing' !!!\n"
]
}
],
"source": [
"# Let's ask user to input a name (character string) and the age (integer) of the person\n",
"\n",
"new_name = input(\"Please enter a name: \") # new_name is a character string\n",
"age = int(input(\"Enter the age of the person above: \")) # age is in integer\n",
"\n",
"print(\"hello world !!\")\n",
"print(\"I hate \" + new_name + \", I believe he is not\", age, \"years old.\")\n",
"print(\"BTW, I think CSCI\" + \"2040\" + \" is REALLY amazing due to 'dynamic typing' !!!\")\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"john c.s. lui, his age is: 18\n"
]
}
],
"source": [
"string1 = \"john c.s. lui\"\n",
"age = 18\n",
"print(string1 + \", his age is:\", age)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Formatted output\n",
"\n",
"Let see how we can do \"formatted\" output."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5000.0 12.0 60000.0 , poor professor!!!\n"
]
}
],
"source": [
"salary = 5000.0\n",
"working_months = 12.0\n",
"print (salary, working_months, salary*working_months, \", poor professor!!!\")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5000.0, 12.0, 60000.0, poor professor!!!\n"
]
}
],
"source": [
"salary = 5000.0\n",
"working_months = 12.0\n",
"print (salary, working_months, salary*working_months, \"poor professor!!!\", sep=\", \")"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5000.0 :-) 12.0 :-) 60000.0 :-) don't marry a poor professor!!!\n"
]
}
],
"source": [
"salary = 5000.0\n",
"working_months = 12.0\n",
"print (salary, working_months, salary*working_months, \"don't marry a poor professor!!!\", sep=\" :-) \")"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"John's monthly salary is: $5000.00, he works for 12.0 months. His annual salary is $60000.00\n",
"What a poor dude!!! \n",
"I bet he can't even afford to buy a small apartment in HK!!! How can he find a wife?\n"
]
}
],
"source": [
"# Formatted output\n",
"salary = 5000.0\n",
"working_months = 12.0\n",
"print (\"John's monthly salary is: $%6.2f, he works for %3.1f months. His annual salary is $%6.2f\" \n",
" %(salary, working_months, salary*working_months))\n",
"print (\"What a poor dude!!!\", \"\\nI bet he can't even afford to buy a small apartment in HK!!!\" + \" How can he find a wife?\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# More formatted output later."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Optional: Jupyter and its installation\n",
"\n",
"The Jupyter Notebook is an open-source web application that allows you to create and share documents
\n",
"that contain live code, equations, visualizations and narrative text. \n",
"\n",
"To install, go to\n",
"- http://jupyter.org\n",
"- Follow the link of \"**Install the Notebook**\" (or http://jupyter.org/install.html)\n",
"- You can install Jupyter (and Python) using Anaconda Distribution (https://www.anaconda.com/download/#macos)
\n",
" This includes Python, the Jupyter Notebook, and other commonly used packages for scientific computing and data science.\n",
"- Or you can install Jupyter using *pip*. Just follow the instruction.\n",
"- Sometimes, you may need to install some Python packages in Jupyter. Please refer:
\n",
" - (https://jakevdp.github.io/blog/2017/12/05/installing-python-packages-from-jupyter/)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Comment about Python\n",
"\n",
"- Interpreter vs. compiler\n",
"- Imperative vs. object-oriented vs. functional programming languages\n",
"- demo of using Python as a simple calculator"
]
},
{
"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.8.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}