CS Paper 1 Flashcards
string manipulation -length
-length of string =
len(string)
spaces are a valid character
string manipulation-position
- stringname>.index(string)
-stringname>.find(string)
string manipulation - substring
-string[start:end]
string manipulation - concatenation
cocatenation - act of joining two/more separate strings
string manipulation - character-character code
supply ord function. ‘a’ returns ASCII coe 97
string manipulation - character code to character
supply the chr function, ‘97’ returns ASCII character ‘a’
string maniupulation commands
random number generator
-import random
print (random.randint(1,6))
exception handling
-process in programming that responds to the occurrence of exceptions(error) during the execution of a program
-goal is to provide a way to detect and report error occurences so that appropriate action can be taken
exception handling -solution
-try clause and jump to except clause when there is an error instead of crashing
subroutines
-block of code given an identifiable name within a program
-use them to break down a larger problem into series of smaller, more manageable problems so they are easier to: code, debug,reuse
procedures
block of code
that takes in 0,1/more parameters
performs a set task
function
block of code
takes in 0/1/more parameters
performs a set task
returns a value
advantages of subroutines
-Programs easier to write
-Programs easier to debug
-Creates reusable components
-Functions can be grouped in a Library for easy reuse across multiple programs
local variables
-Declared inside a Subroutine
-Only accessible by that subroutine
-Created when subroutine is called
-Destroy destroyed when subroutine ends