INF1511 notes.pdf
Document Details
Uploaded by Deleted User
Tags
Related
- Csc 102 Lecture 4: Introduction to Programming (Python) PDF
- Advanced Programming in Python PDF
- ITP 313 Python Fundamentals and Data Types PDF
- BSc (Hons) Electronics Ist Sem - Programming Fundamentals using Python PDF
- Meeting 2 - Fundamentals of Python Programming - s.en.ar.pptx PDF
- Python Data Types PDF
Full Transcript
Unit 1 Python is an interpreted language –rapid flexible, can be incorporated with other languages IDLE (Integrated Development Environment) Data Types in Python Python has a rich set of fundamental data types. The operations that are applicable on an object depend on its data type (i.e., an obje...
Unit 1 Python is an interpreted language –rapid flexible, can be incorporated with other languages IDLE (Integrated Development Environment) Data Types in Python Python has a rich set of fundamental data types. The operations that are applicable on an object depend on its data type (i.e., an object’s data type determines which operations are applicable on it). The list of data types are as follows: Integers: Integers are 32 bits long, and their range is from –2 32 to 2 32 – 1 (i.e., from – 2,147,483,648 to 2,147,483,647). Long Integers: It has unlimited precision, subject to the memory limitations of the computer. Floating Point Numbers: Floating-point numbers are also known as double-precision numbers and use 64 bits. (decimals) Boolean: It can hold only one of two possible values: True or False. Complex Number: A complex number has a real and an imaginary component, both represented by float types in Python. An imaginary number is a multiple of the square root of minus one, and is denoted by j. For instance, 2+3j is a complex number, where 3 is the imaginary component and is equal to 3 × √−1. Squaring a number in Python is straightforward. You can accomplish this using the exponentiation operator ( ** ). You can get the square of 5 like this: 5 ** 2. In this example, we've defined a variable num and assigned it the value 5 Strings/Literals: Sequences of Unicode characters. Lists: Ordered sequences of values. {2,9,4} Tuples: Ordered, immutable sequences of values. (‘apple’, ‘mango’, ‘banana’) immutable not changeable Sets: Unordered collections of values. Dictionaries: Unordered collections of key-value pairs. Note Unicode is a standard that uses 16-bit characters to represent characters on your computer. Unlike ASCII (American Standard Code for Information Interchange), which consists of 8 bits, Unicode uses 16 bits and represents characters by integer value denoted in base 16. A number does not include any punctuation and cannot begin with a leading zero (0). Leading zeros are used for base 2, base 8, and base 16 numbers. For example, a number with a leading 0b or 0B is binary, base 2, and uses digits 0 and 1. Similarly, a number with a leading 0o is octal, base 8, and uses the digits 0 to 7, and a number with a leading 0x or 0X is hexadecimal, base 16, and uses the digits 0 through 9, plus a, A, b, B, c, C, d, D, e, E, f, and F. An object that can be altered is known as a mutable object, and one that cannot be altered is an immutable object Can use ‘ and “ Variables choose a name and then use = (can be letters numbers or words) Keywords - lower case Comments: story # infront of sentence Continuation lines Physical line – see in program Logical line – single statement Statement too long for one line can join using \ if no comment is in the next line add onto one line (cant get this work) Also joins if an open parenthesis ( ( ), bracket ( [ ) or brace ( { ) is not closed Printing – function Add a space in between + “ “ + or comma and space 1. print("10"+ " " + "10") 2. print("10", "10") if a run on sentence use the right amount of ‘ “ for the sentences and remove in the middle Decimal points.2f Learn Decimal %.2f ' %fSpeed) Whats is this thetotal=a//b theremain=a%b 1. try to understand what is a data type, in simple terms is a type of data stored in a container/variable e.g int for numbers, bool for true or false 2. data type determine the operation we can perform on the container/variable the data is stored in e.g int num1 =5 ; int num2 =7; num1 and num2 are containers/variable with values 5 and 7. so we can perform addition operarion on these conteiners, e.g sum = num1 + num2 = 5+7 3. it also determine the size of a container/variable, e.g int data type does not reserve the same memory size as bool or string or char 4. it also determine the meaning of the values inside the container/variable, if a container/variable is of type int you know that it holds a number, if bool it holds a true or false value Automate boring stuff – videos Expression (values + operators) 2+2 enter = expression, basic instruction, reduce to single value Values is 2(numbers) Operator - + * / (cant use x) Order of operation ()*/+- can override by using brackets Data type Integers – 2, 3 Floating – decimals 3.14, 40.0 Strings – use quotes ‘ ’ (contain text, start and end in quotes) concatenation - ‘Alice’ + ‘Bob’ = AliceBob ‘Alice’* 3 = AliceAliceAlice String concatenation ‘Hello’ + ‘!” * 10 Variables (box) stores (values can use in expression spam = 42 (string) spam + ' World' (space in front of word creates space) Statements – dont evaluate to single value but can have expression inside spam=10 spam=spam+1 spam #must state what you want in return operat Evaluates into single value = expression Doesn't evaluate = statement (instructions or code) The execution – which instruction is being currently executed Comments doesn't print, its reminder for you Blank lines are ignored – can use to space it out for yourself print() function – print out text Functions = mini program in the programs Arguments - print(‘Hello world!’) = values passed to functions print(input) - this will request the user to enter something (store the name) print(len)= check the length of characters (runs string and returns an integer) str() - can't do math on a string. Can't do string concatenation on integer int() - show values float() - decimals, if put integer 3 will result in 3.0 print(str('27')+' years old') If already have a number don't put int, only out int if you have defined the input Flow chart how execution flows, decision making (flow control statements and conditions) Boolean operators True and false (expression use Cap T and F) Infinite numbers Comparison operators == Equal to != Not equal to < Less than > Greater than = Greater than or equal to Integer and string will always be false even if have a number Integers and float can be equal Boolean values And or not Truth tables True and True = True True and False = False False and True = False False and False = False True or True = True True or False = True False or true = True False or False = False Not True = false Not false = True Unit 2 TB Performing arithmetic operations X + y = addition X – y = subtraction X * y = multiplication X / y = true division (return integer if both are integers) X //y =truncating division (floor division), result integer ignore remainder works with int and float X **y = exponentiation sets x to power y X % y = modulo operator (returns remainder) return up to 6 decimal places %f -x = unary minus +x = unary plus p=q=r=10 a=1.0/3.0*(p+q+r) print('Average of 3 variables is', a) Average of 3 variables is 10.0 (1 decimal) print('Average of 3 variables is %.2f' %a) Average of 3 variables is 10.00 (2 decimal) print('Average of 3 variables is %d' %a) Average of 3 variables is 10 (no decimal) from __future__ import division =( avoid getting incorrect truncating integer %.2f' %a = (2 decimal) %d' %a = (no decimal) = floating point truncated Exponentiation- use ** double asterisks Can use pow() = pow(a, b) same as a**b Can use from math import pi to use the math module value of pie Multiple assignment statement- basic assignment can do more than assign a result of a single expression can do multiple p,q,r=10,20,30 Sum, ave=p+q+r, (p+q+r)/3 Left and right to match = each will assign in order Usining escape sequences –special for non printing characters (tabs, newlines)begin with backlash \ Escape Character Description \a Bell (beep) \b Backspace wherever the cursor is it backspaces as many entries print('Festival Discount\b\b\b\b\b\b\b\b\b Offer') Festival offerunt \f Form feed \n Newline (next line) print('Hello World\nIt\'s hot today') \r Carriage return \t Tab \v Vertical tab (put large spaces inbetween words) \\ Literal backslash \' Single quote (it’s) print('It\'s hot today') \" Double quote - display it in quotes print("He said: \"I am going \"") Finding a data type Type(10) = integer Type(‘Hello’) = string a=10 (integer) b=15.5 (float) c="Hello" (string) d=[2,9,4] (list) e=('apple', 'mango', 'banana') (tuple) f=True (bool) Displaying octal and hexa values a=0o25 = octal(decimal) %o and oct() b=0x1af = hexa %x and hex() Getting data- inputs Ask for a answer and uses it as a variable Auto conversion (coercion) If one variable is int the other will automatically become the same. Int() - no decimals Operands=variables Bitwise operators( only use in integers and long integers) binary operators X >y = binary shift right same as x / 2**y X & y = bitwise AND = compared 1 or 0 X | y = bitwise AND = compares its either 0 or returns as 1 X ^ y = bitwise exclusive AND = compares its either 1 or returns as 0 if the is the same ~ x = bitwise inversion = 1 is converted to 0 or other way around Complex numbers combo if real and imaginary component as floating type 3+1.2j 3 = real (input is = real) 1.2j =imaginary (1.2 x square –1) (input is = imag) a = 3.0 + 1.2j b= -2.0 - 9.0j Can add the int and add the imaginary (displayed in () Making decisions If and else = control flow Decides which block of statements in executed based on logical expression Small indentation needed for print If expression is true = executed if not otherwise us else statement. Else is optional m=int(input("enter grades:")) if(m>60): print("first division") else: print('second division') Comparison operators Operator Meaning < Less than > Greater than = Greater than or equal to == Equal to != Not equal to **remember : (no space) after if and else and indents m=int(input("enter grades:")) if(m>60): print("first division") else: =elif(m>=45) if(m>=45): print('second division') else: print('third division') If-elif-else statement – if have multiple expression but want to execute specif set of code block. Avoid excessive indentation If + else = elif Logical operators AND – return as true if all is true OR – return as true if all is true NOT – negated if true becomes false if(m>=45 and m