#!/usr/bin/env python # coding: utf-8 # # Data Values & Types, Variables, Std I/O, Computations # - A value is one of the fundamental things -- like a letter or number -- that a program manipulates. # - These values are classified into data types. # # - In Python, there're mainly two data types: numbers and strings - numbers can be integer or float # - Boolean (True/False) is also supported type! # # ## first program - hello world! # In[1]: # First program # This is a comment line... # By Programmer info... print('Hello world!') # In python 2: print is a statement not a function # print 'Hello World!' # ## comments # - Programming languages provide a way to write comment along with the codes # - Python uses # symbol to write a single line comment # - comments are for humans/programmers to convey/explain what the code is doing or supposed to do # - Python interpreter ignores the comments # ## data types # - built-in type() function can be used to know type of data # - functions will be covered in Chapter 04 # In[2]: type(100) # In[3]: type(1000.99) # In[4]: type('Hello World!') # In[5]: type('A') # In[6]: print hello # In[22]: type("17") # In[23]: type("""Triple double quoted data""") # In[24]: type('''Type of Triple single quote data is''') # ## type conversion/casting # - changing data from one type into another when possible # - use built-in function such as str(), int(), float(), etc. to convert data to that particular type # # In[25]: data = 'hello' # can't conver it to int or float types # In[26]: data = '100' type(data) # In[27]: num = int(data) type(num) # In[28]: price = float('500.99') type(price) # In[29]: num = 99.99 strNum = str(num) type(strNum) # ## standard output # - printing or writing output to common/standard output such as monitor # - way to display the results and interact with the users of your program # In[30]: print('''"Oh no", she exclaimed, "Ben's bike is broken!"''') # In[31]: print("What\'s up\n Shaq O\'Neal?") # In[32]: type(True) # In[33]: print(x) # In[7]: help(str) # ## escape sequences # - some letters or sequence of letters have special meaning to Python # - single, double and tripple single or double quotes represent string data # - use backslash \ to represent these escape sequences, e.g., # - \n - new line # - \\ - back quote # - \t - tab # - \r - carriage return # - \' - single quote # - \" - double quote # In[34]: print('hello \there...\n how are you?') # In[35]: print('how many back slahses will be printeted? \\\\') # In[36]: print(r'how many back slahses will be printeted? \\\\') # ## variables # - Variables are identifiers that are used to store values which can be then easily manipulated # - variables give names to data so the data can be easily referenced by their names over and again # - Rules and best practices for creating identifiers and variable names: # - can't be a keyword -- what are the built-in keywords? # - can start with only alphabets or underscore ( _ ) # - can't have symbols such as $, %, &, white space, etc. # - can't start with a digit but digits can be used anywhere else in the name # - use camelCase names or use _ for_multi_word_names # - use concise yet meaningul and unambigous names for less typing and avoid typos # In[11]: help('keywords') # In[12]: help('True') # In[13]: # Define some variables to show the values stored in variables can be changed from one type to another # Python is loosely typed # ## computation - operators and operands # - **operators** are special tokens/symbols that represent computations like addition, multiplication and division # - the values an operator uses are called **operands** # - some binary operators that take two operands # - addition: 10 + 20 # - subtraction: 20 - 10 # - true division: 10 / 3 # - integer division: 10 // 3 # - remainder: 10 % 2 # - power: 2 ** 3 # In[14]: # play with some examples of various operators supported by Python # ## order of operations # depends on the rules of precedence: PEMDAS from high to low order
# 1. Parenthesis # 2. Exponentiation # 3. Multiplication and Division (left to right) # 4. Addition and Subtraction (left to right) # # In[15]: # some examples print(2 * 3 ** 2) # ## operations on string # - Chapter 08 covers more on string data type # In[16]: help(str) # In[17]: # some examples name = "John" print(name, 'islower', name.islower()) print(name, 'isalpha', name.isalpha()) print(name, 'upper', name.upper()) print(name.zfill(10)) print(name.swapcase()) print(name.startswith('j')) print(' hello world'.split()) print('hello$world'.split('$')) # ## standard input # - read data from standard or common input such as keyboards # - allows your program to receive data during program execution facilitating user interactions # In[18]: name = input('What is your name? ') # In[19]: num = input('Enter a number =>') print('You entered: ', num) # ## composition # - break a problem into many smallers sub-problems or steps using high-level algorithms # - incrementally build the solution using the sub-problems or steps # ### exercise # # write a program that finds area and perimeter of rectangle # In[20]: # Demonstrate composition step by step # Algorithm steps # ### excercise # write a program that finds perimeter and area of a circle.

# # In[21]: # Demonstrate composition step by step # Algorithm steps