Introduction to Computer Science Programming with Python Motivation




























- Slides: 28
Introduction to Computer Science Programming with Python
Motivation ▪ According to Indeed, the average salary for a Python Developer is $120, 704 per year in the United States ▪ While most experienced workers make up to $205, 197 per year ▪ According to TIOBE, the software quality company, the Python Programming Language is one of the highest positions (since 2001) – January, 2019
Motivation
Motivation ▪ And it continues to rise – https: //www. tiobe. com/tiobe-index
Introduction ▪ Computers are meant to do things for us ▪ But, we need to speak their language to describe what we want done ▪ What is a programmer? ▪ A programmer is able to develop programs/set of instructions that tell the computer/hardware what they are supposed to do ▪ Intermediate between the hardware and the end user ▪ What is code? Program? Software? ▪ A sequence of stored instructions ▪ It is a little piece of our intelligence in the computer
Users versus Programmers ▪ Users see computers as set of tools – word processor, spreadsheet, map, browser, etc. ▪ Programmers learn the computer language ▪ Programmers have tools that build new tools ▪ Programmers automate tasks for many users, and sometimes for themselves
Hardware Overview – Generic Computer Software Input & output devices What next? Central Processing Unit Main Memory 0101000 0110001 Secondary Memory
Getting started with Python Programming with Python
History ▪ 1991 by Guido van Rossum ▪ Indentation to delimit code blocks ▪ Dynamic types ▪ Auto memory management ▪ Object Oriented Programming Language ▪ Interpreted (can compile to byte codes) ▪ Can be easily extended via C/C++
Requirements Python 3 ▪ Pre-installed on Macs ▪ Check version and update to 3 if necessary – terminal: python --version or python 3 --version ▪ Windows and Mac. OS ▪ https: //www. python. org/downloads/relea se/python-370/ ▪ Linux: ▪ sudo apt update && sudo apt upgrade ▪ sudo apt install python 3 Integrated Development Environments (IDEs) and Text Editors ▪ Recommended: Py. Charm by Jet. Brains, free license for students: ▪ https: //www. jetbrains. com/student/ ▪ Atom – itom. io Run, Test, and Verify ▪ Terminal: python 3 ▪ Py. Charm: Setting/Preferences Project Interpreter Add System Interpreter Python 3
Welcome to Python 3 Python Console
Welcome to Python 3 Python Console
Welcome to Python 3 Python Console Note, this is a good test to ensure that you have Python correctly installed. Use quit() or exit() to end the interactive session
Welcome to Python 3 Python Console – Syntax Error/Name. Error is when the compiler does not know what to do with the given script.
Sample Program Count words in a file in Python Just a sample program! We will be able to develop alike programs soon!
Reserved Words - Vocabulary Sentences or lines x=2 Assignment statement x=x+2 Assignment with expression print(x) Print function variable Operator Constant Function ▪ In Python, we may use ; (semicolon) to separate more than one statement per line (but don’t) ▪ For example: x=2 print(x) is the same as: x = 2; print(x)
Welcome to Python 3 ▪ print(”Hello world”) ▪ print(“Hello “, user) ▪ What is the result of the above print statement? ▪ # this is a comment. Usually, use for single line comment ▪ Three double quotes informs the compiler to ignore the following commands. Usually used for multiple line comments. E. g. , ”””the following line is used to print the user’s name””” ▪ exit()
Welcome to Python 3 ▪ print – by default, always includes a new line at the end ▪ Use print(“Hello”, end=“”) to change that ▪ Or, end the line with a dot – print(”Hello”, end=“. ”) ▪ Quotes – ▪ no difference between a single or double quote. E. g. , “hello” or ‘hello’ ▪ Triple quotes are treated as regular text with the exception that they can span multiple lines. If they are not assigned to a variable, they will automatically garbage collected as soon as we compile Python file ▪ End of the line continuation ▪ ”This is the first sentence. ▪ This is the second sentence. ” Equivalent This is the first sentence. This is the second sentence
Python Scripts ▪ Inactive Python is good for experiments and programs of 3 -4 lines long ▪ Most programs are much longer, so we type them into a file and tell python to run/compile the commands in the file. Thus, an IDE could be very helpful, lets remember the recommended IDE, Py. Charm ▪ As a convention, we add “. py” as the suffix (file format) on the end of files that contain python script to indicate that they contain Python ▪ It is much more common and easier to write in a file or IDE ▪ IDE’s include helpers to check spelling and auto completion
Live Demo ▪ Python Console ▪ Compile/run a python script – print(“Hello! Introduction to Computer Science student”) ▪ Compile/run a python file via terminal ▪ Getting Started with Py. Charm ▪ System Interpreter (python 3) ▪ Python File ▪ Compile/Run
Reserved Words - Vocabulary ▪ You cannot use reserved words as variable names / identifiers False class return is finally None if for lambda continue True def from while nonlocal and del global not with as elif try or yield assert else import pass break except in raise
Brief introduction to variables ▪ x = 10 ▪ Ameen_salary = x * 0. 8 ▪ Ameen_firstname = “Ameen” ▪ Text input is identified as string in most programming languages. ▪ Thanks to Python, data types are automatically determined ▪ Strings are always placed between double quotes
Brief introduction to Conditional Steps
Brief introduction to Repeated Steps Loops (repeated Steps) have iteration variables that change each time through a loop
Brief introduction to Repeated Steps Loops (repeated Steps) have iteration variables that change each time through a loop
Indentation ▪ Whitespaces are important in Python ▪ Error for incorrect spacing ▪ Tab or space allowed ▪ Spacing indicates nesting
Compiler ▪ A compiler is a program/set of programs that transforms source code written in a programming language (source language) into another computer language (the target language, which is machine language that is having a binary form known as object code) ▪ Transforming code is required to execute programs ▪ Translates from high-level language to lower-level language (machine code). (Python is a high-level language) ▪ Decompiler – a program that translates from low-level language to higher-level language.
Language Translator/Converter ▪ A program that translates between high-level languages