Tips for ICS20 Python

Python has five standard data types −

  • Numbers
  • String
  • List
  • Tuple
  • Dictionary

You can only print strings.

You can convert other variable types to string by using str() function

enumerate() instead of using index.

Example:

List

list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]

Tuples

Tuples are like lists but us round brackets and can be though of as read only lists.

tuple = ( 'abcd', 786 , 2.23, 'john', 70.2  )

Dictionary 

Dictionary is an associative array

tinydict = {'name': 'john','code':6734, 'dept': 'sales'}

To retrieve the data from the dictionary, you can use either of these two approaches:

tinydict['name']

tinydict.get('name', 'Not Found') # this method is cleaner at handling error conditions. It returns Not found if the 'name' doesn't exist

Screen Output Methods

Assignment 1 Question 3

4 different print methods. Plain print() and then with each of the following:

print()

  • .title
  • .upper
  • .lower

Assignment 2

Function Definitions: def function_name(passed_parameter)

function return

int() like str()

len()

.split() - to separate text by a definied separator

for loops

for Something in <name of list> cause the software to iterate through each item in the list

enumerate() provides converts a list to a numerically indexed array

.append() - add contents of append to the end of the list

reversed() - allows you to iterate through a list in reverse order when using a for loop

slices of lists - alows you to get specific parts of a list listname[start_index: end_index]  - listname[:3] gives you the list up to but not including element number 3. listname[3:] gives you everything from 3 to the end.
https://towardsdatascience.com/python-basics-6-lists-and-list-manipulation-a56be62b1f95

Question 2

.remove(value) to remove item that has value of 'value' from a list

del(listname[x]) removes the item at index x from the list

Question 3

list comprehension https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions

List Comprehensions Video: https://www.youtube.com/watch?v=AhSvKGTh28Q 

List comprehensions are a more efficient way of populating lists that effectively allow you to combine some action with one or more for loops. 

A list comprehension consists of brackets containing an expression followed by a for clause, then zero or more for or if clauses. The result will be a new list resulting from evaluating the expression in the context of the for and if clauses which follow it. For example, this listcomp combines the elements of two lists if they are not equal:

Example:

squares = [x**2 for x in range(10)]

Substrings is done in the same way slices act on lists. [x:y] 

Error in Assignment: Question 1: "The reverse alphabetical order of the list is ['c', 'b', 'a', 'abc3', '3']" should be ['3', 'abc3', 'c', 'b', 'a']

Error in Assignment: Question 3: Should say using comprehensions, not say (not using a for loop).

Assignment 4

x = randint(1, 100) any random number between 1 and 100

Error in Assignment: Clarity: "Print out the two outcomes as well as the Sum" should say "Print the outcome of the two dice rolls as well as ..."

Demo Ligth Dependent Resistor

Error in Assignment: The assignment doesn't describe how to wire it properly.

Connect the positive power (+5V Pin 2) pin to one side of the CdS cell.

Connect the GPIO 4 (pin 7) to between the capacitor and the CdS cell.

Connect the other end of the capacitor to the ground (pin 6)

Other Pything stuff.

len()

\n for line return

e = sentence.find("a", 3, 20)
print(e)

.startswith() - returns true if the provided string starts with a string segment.

.count()

numbers.isnumeric()

format()

if / elif is the replacement for case statements in PHP

 

pull_up_down=GPIO.PUD_DOWN  this is used in a GPIO.setup statement to set the begining state of a pin. If this isn't set, the pin could show an input or output voltage that is unpredicted. It might be on when you expect it to be off and off when you expect it to be on. Since you're likely using this input as a signal for something else, you want to know the state of it from the begining.

 

 


Print