Lecture 2 Getting Started DA5120 – Programming for Business Analytics Supun Gothama
Python
Python Programming Language The Python programming language was first released in 1991 Python is an interpreted, high-level programming language for general-purpose programming Design philosophy includes: Code readability Expressing concepts in small amount of code
Installing Python Python is available to download from the official website https://www.python.org/downloads/ In this module we will be using Python 3.7 version
Using Python Python interactive mode Work with the interpreter interactively Enter expressions, statements, commands one at a time, and wait for the interpreter to show results
Using Python Python scripting mode Write the Python program in a text file Invoke the interpreter with the program file as input
Hello World Let’s write our first Python program in the Python interactive mode >>> print( "Hello World" ) Hello World This is probably the simplest program you can write
Variables
Variables In Python, variables are names assigned to values Used to refer to that value throughout the code Think of variables as a name given to containers that holds data Values are stored in the computer’s memory Variable name is reference to the memory location Example in Python x = 25 65 y 25 x
Assigning Variables Values are assigned to variable names using the equal sign (=) Known as the Assignment Operator The operand to the left of the = operator is the name of the variable The operand to the right of the = operator is the value stored in the variable
Assigning Variables Once assigned, variable name can be used to refer the value message = "Python is fun" print(message) Python is fun
Assigning Variables The assignment operator '=' used in writing computer programs does not have the same meaning as mathematics Don’t mix them up! Think as, variable ← value
Variable Naming We cannot use arbitrary strings as variables names The Python variable naming rules are: Must begin with a letter (a - z, A - Z) or underscore (_) Other characters can be letters, numbers or _ only Names are case sensitive. i.e. ‘a’ and ‘A‘ are 2 different variables Reserved words cannot be used as a variable name
Variable Naming Following reserved key words cannot be used as python variable names and def FALSE import not TRUE as del finally in or try assert elif for is pass while break else from lambda print with class except global None raise yield continue exec if nonlocal return
Which of these variable names are valid ? Activity Sales PROFIT return annualRate tax-id tax# 1stName _name
Which of these variable names are valid ? Activity Sales ✔ PROFIT ✔ return ❌ keyword annualRate ✔ tax-id ❌ ‘ -’ not allowed tax# ❌ ‘ #’ not allowed 1stName ❌ starting with 1 _name ✔
Variable Naming A name should be meaningful “price” is better than “p” Generally lowercase (simple letters) For a multiple-word name, use either the underscore as the delimiter or use camelCase capitalization Eg : total_amount or totalAmount Pick one style and use it consistently throughout your program Shorter meaningful names are better than longer ones
Constants When we have a variable with value that should not change, we call it a constant We usually define them in all UPPER CASE letters Separate multiple words with an underscore Example: PI = 3.141592 BIRTH_RATE = 17.58 GOLDEN_RATIO = 1.6180
Constants Capitalized names aid the programmer to distinguish them from variables It is up to the programmer to recognize a named constant and not to change it