Programming Raspberry Pi 3 & 4 for Beginners

857 179 19MB

English Pages [225] Year 2019

Report DMCA / Copyright

DOWNLOAD FILE

Polecaj historie

Programming Raspberry Pi 3 & 4 for Beginners

Citation preview

PROGRAMMING RASPBERRY PI 3 & 4 FOR BEGINNERS

CHARLES

SMITH

All rights reserved. No part of this publication Programming Raspberry Pi 3 & 4 for Beginners may be reproduced, stored in a retrieval system or transmitted in any form or by any means - electronic, mechanical, photocopying, recording, and scanning without permission in writing by the author.

Printed in the United States of America © 2019 by Charles Smith

Churchgate Publishing House USA | UK | Canada

i

ii

iii

iv

v

vi

I

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

T

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

I

43

44

45

>>> a = 5 >>> b =6 We can’t say; a = b b = a

>>> a = 5 >>> b = 6 46

>>> >>> >>> >>>

temp = a b = temp print (a) print (b)

>>> a = a + b >>> b = a – b >>> a = a – b

>>> Print (a) >>> Print (b)

>>> >>> >>> >>> >>>

a = a b = a a = a Print Print

^ b ^ b ^ b (a) (b)

>>> a,b = b,a >>> Print (a) >>> Print (b) 47

ROT_TWO().

>>> message = meet me this afternoon >>> print (message) >>> meet me this afternoon

48

>>> x = 2 >>> y = 3

>>> >>> >>> 5 >>>

x = 2 y = 3 x + y x – y 49

>>> >>> >>> >>> >>> >>>

-1 x*y 6 x/y 0.6666666666666

>>> x = 2 >>> x = x + 2 4

>>> >>> 4 >>> >>> 6 >>>

x = 2 x = x + 2 x += 2 x

#note that x was 4 initially and when we increment by 2 we have 6

>>> x = 6 >>> x *= 3 18 >>> 50

>>> a = 5 >>> b = 6 Or we can assign it thus; >>> a,b = 5,6 >>> a 5 >>> b 6 >>>

>>> >>> 7 >>> >>> >>> >>> -7

n = 7 n -n -7 n = -n n

>>> a = 5 >>> b = 6 51

>>> a >>> b >>> a True >>> a False >>>

= 5 = 6 < b > b

Example: >>> a = 5 >>> b = 6 >>> a == b False >>> >>> a = 7 >>> b = 7 >>> a == b True >>>

>>> a = 7 >>> b = 7 >>> b >> >>> a = 7 >>> b = 7 52

>>> b => b True >>>

>>> a = 8 >>> b = 7 >>> a != b True >>> >>> a = 7 >>> b = 7 >>> a != b False >>>

>>> a = 5 53

>>> b = 4 >>> a < 8 and b < 5 True >>>

>>> a >>> b >>> a True >>> a

= 5 = 4 < 8 and b < 5

>>> a >>> b >>> a True >>> a False >>>

= 5 = 4 < 8 and b < 5

< 8 and b < 2

< 8 and b < 2

54

>>> a = 5 >>> b = 4 >>> a < 8 or b < 2 True >>>

>>> x = True >>> x True >>> not x False >>> x = not x 55

>>> x False >>>

Python 3.7.0 (v3.7.0:1bf9cc5093, June 27 2019, 04:06:47) [MSC v.1914 32 bit (intel)] on win32 Type “copyright”,”credits” or “license()” for more information. >>> bin(25) #click on ‘Enter Key” on the Keyboard ‘0b11001’ # ‘0b’ indicates binary format. >>>

56

>>> 0b0101 5 >>>

>>> oct(25) ‘0o31’ # ‘0o’ indicates octal format >>>

>>> hex(25) ‘0x19’ # ‘0x’ indicates hexadecimal format >>>

>>> hex(10) ‘0xa’ # ‘0x’ indicates hexadecimal format >>>

>>> 0xf 15 >>>

(&) (|) (^) 57

()

58

>>> ~ 12 -13 >>>

59

>>> 12 & 13 12 >>> 25 & 30 24 >>> 60

12 13

-

00001100 | 00001101 00001101

>>> 12|13 13 >>>

61

13

>>> 12^13 1 >>> 25^30 7 >>>

62

>>> 10>

63

>>> 10>>2 2 >>>

64

>>> num = 2.5, where 2.5 is a float. How do we know the number is a float? Let’s say; >>> num = 2.5 >>> type (num)

>>>

>>> num = 5 >>> type (num)

>>>

>>> num = 6+9j 65

>>> type(num)

>>>

>>> a = 5.6 >>> b = int (a)

>>> a = 5.6 >>> b = int (a) >>> type (b)

>>> b 5 >>>

>>> k = float(b) >>> k 5.0 >>> >>> k = 6 >>> c = complex (b,k) >>> c (5+6j) >>> 66

It says 5+6j where 5 is your b and 6 is your k.

>>> b>>

>>> z = b>> z True >>> If we want to determine the variable class type thus; >>> z = b>> z True >>> type (z)

>>> . >>> z = b>k False >>> 67

>>> int (True) 1 >>> int (False) 0 >>>

>>> lst = [25,12,36,95,14] >>> type(lst)

68

>>> nums = [0] 25 >>> nums = [4] 14 >>> nums = [2] 36 >>>

>>> nums[2:] [36,95,14] >>>

69

>>> nums[-1] 14 >>> nums[-5] 95 >>>

>>> names = [‘martins’, ‘smith’, ‘John’] We can also have a list with different data, for instance, float, string and integer. >>> values = [9.5, ‘smith’, 24]

>>> nums = [25,12,36,95,14] >>> names = [‘martins’, ‘smith’, ‘John’] >>> twolists = [nums, names] >>> twolists [[25,12,36,95,14],[‘martins’, ‘smith’, ‘John’]]

[[25,12, 36,95,14], [‘martins,’ ‘smith,’ ‘John’]]

70

>>> nums = [25,12,36,95,14] >>> nums.append(45) >>> nums [25,12,36,95,14,45] >>> nums

>>> nums = [25,12,36,95,14,45] >>> nums.insert(2,77) >>> nums [25,12,77,36,95,14,45]

71

>>> nums = [25,12,36,95,14,45] >>> nums.remove(14) >>> num [25,12,77,36,95,45]

>>> nums = [25,12,36,95,14,45] >>> nums.pop(1) 12 >>> nums [25,77,36,95,45]

72

>>> nums = [25,12,36,95,14,45] >>> nums.pop() 45 >>> nums [25,12,77,36,95,14]

>>> nums = [25,12,36,95,14,45] >>> del nums[2:] >>> nums [25,12,] >>>

>>> nums = [25,12,36,95,14,45] 73

>>> nums.extend([29,12,14,36]) >>> nums [25,12,36,95,14,45,29,12,14,36] >>>

>>> nums = [25,12,36,95,14,45] >>> nums.sort() >>> nums [12,14,25,36,45,95] >>>

>>> nums.clear

>>> nums = [25,12,36,95,14,45] >>> min (nums) 12 74

>>>

>>> nums = [25,12,36,95,14,45] >>> max (nums) 95 >>>

>>> nums = [25,12,36,95,14,45] >>> sum (nums) 227 >>>

>>> s = {25,36,45,15,12,25} >>> s 75

{36,12,45,15,25} >>>

>>> s = {25,36,45,15,12,25} >>> s {36,12,45,15,25} >>> type(s)

>>>

>>> t = (25,36,4,57,12) >>> type(t)

>>> 76

77

Import array as arr Arr.array[]

from array import *

78

from array import * vals = array [‘i’,[5,9,8,4,2]]

from array import * vals = array [‘i’,[5,9,8,4,2]] print(vals) array [‘i’,[5,9,8,4,2]]

from array import * vals = array [‘i’,[5,9,8.5,4,2]] print(vals) TypeError: integer argument expected, got float

79

from array import * vals = array [‘i’,[5,-9,8,4,-2]] print(vals) array [‘i’,[5,-9,8,4,-2]]

from array import * vals = array [‘I’,[5,-9,8,4,-2]] print(vals) overflowError: can’t convert negative values to unsigned int.

80

from array import * vals = array [‘i’,[5,9,-8,4,2]] print(vals.buffer_info()) (59741056, 5)

vals = array [‘i’,[5,9,-8,4]], buffer_info function will give us

(16303920, 4),

81

print(vals.typecode()) for the array [5,9,-8,4]],

from array import * vals = array [‘i’,[5,9,-8,4,2]] print(vals.typecode()) i

from array import * vals = array [‘i’,[5,9,-8,4,2]] 82

vals.reverse() print(vals) array [‘i’,[2,4,-8,9,5]]

from array import * vals = array [‘i’,[2,4,-8,9,5]] print(vals[0]) 2 from array import * vals = array [‘i’,[2,4,-8,9,5]] print(vals[1]) 4

for i in range (5): print(vals[i]) 5 9 -8 4 2 83

for i in range (len(vals): print(vals[i]) 5 9 -8 4 2

Vals = array (‘i’, [5,9,8,4,2]) newArr = array(vals.typecode, [a for a in vals]) for a in newArr: print (a)

5 9 8 4 2

(vals.typecode, indicate is, 84

[a for a

in vals])

(1)

vals.typecode -

(2)

[a for a in vals] –

Vals = array (‘i’, [5,9,8,4,2]) newArr = array(vals.typecode, [a*a for a in vals]) for a in newArr: print (a) 25 81 64 16 4

from array import * vals = array [‘u’,[‘a’,’e’,’i’]] for i in range (len(vals): print(vals[i]) a e i

85

i = 0 while i