Skip to content

Commit 705df3d

Browse files
EV3 examples
1 parent d7f5ace commit 705df3d

File tree

9 files changed

+149
-0
lines changed

9 files changed

+149
-0
lines changed

bytesExample.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Bytes - immutable sequence of bytes
2+
3+
#bytes literals
4+
bytes1 = b'Python3'
5+
bytes2 = b"Python3"
6+
7+
print(bytes1)
8+
print(bytes2)
9+
10+
str1 = "Sample text to encode"
11+
data = str1.encode('utf-8')
12+
val = data.decode('utf-8')
13+
14+
print(str1)
15+
print(data)
16+
print(val)
17+
print(val == str1)

complexTypes.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+

dictExample.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# dictionary - mutable mapping of keys to values
2+
3+
telephoneDicts = {
4+
'babu': '425-241-7667',
5+
'srujana': '425-503-3106'
6+
}
7+
8+
print(telephoneDicts)
9+
telephoneDicts['babu'] = '206-241-7667'
10+
print(telephoneDicts)
11+
12+
telephoneDicts['anvi'] = '206-555-7667'
13+
print(telephoneDicts)
14+
15+
#empty dictionary
16+
emptyDict = {}
17+
print(emptyDict)

ev3Led.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env python3
2+
from ev3dev2.led import Leds
3+
from time import sleep
4+
5+
leds = Leds()
6+
7+
leds.all_off()
8+
sleep(1)
9+
10+
leds.set_color('LEFT', 'AMBER')
11+
leds.set_color('RIGHT', 'AMBER')
12+
sleep(4)
13+
14+
#With custom color:
15+
leds.set_color('LEFT', (1, 0)) # Bright RED
16+
leds.set_color('RIGHT', (0, 1)) # Bright Green
17+
sleep(4)
18+
19+
20+

ev3LineFollower.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env python3
2+
3+
from ev3dev2.motor import MoveTank, OUTPUT_A, OUTPUT_D
4+
from ev3dev2.sensor.lego import ColorSensor
5+
from ev3dev2.button import button
6+
from time import sleep
7+
8+
btn = Button()
9+
tank_pair = MoveTank(OUTPUT_A, OUTPUT_D)
10+
11+
c1 = ColorSensor()
12+
13+
while not btn.any():
14+
if c1.reflected_light_intensity < 25:
15+
tank_pair.on(left_speed = 50, right_speed = 0)
16+
17+
else:
18+
tank_pair.on(left_speed = 0, right_speed = 50)
19+
20+
sleep(0.1)
21+

ev3TouchSensor.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env python3
2+
from ev3dev2.led import Leds
3+
from ev3dev2.sensor.lego import TouchSensor
4+
from time import sleep
5+
6+
leds = Leds()
7+
ts = TouchSensor()
8+
9+
while True:
10+
if ts.is_pressed:
11+
leds.set_color('LEFT', 'RED')
12+
else:
13+
leds.set_color('LEFT', 'GREEN')
14+
15+
sleep(0.1) # Give the CPU some rest

forLoopEx.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
cities = ["London", "New York", "Madras", "Bangalore"]
2+
3+
for city in cities:
4+
print(city)
5+
6+
telephones = {
7+
"babu": "425-241-7667",
8+
"srujana": "425-503-3106",
9+
"amogh": "206-555-5523"
10+
}
11+
12+
for telephone in telephones:
13+
print(telephone, telephones[telephone])

listExample.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# List - mutable sequences of objects
2+
3+
c = list("convert string to list")
4+
5+
print(c)
6+
7+
lst = [
8+
"Python",
9+
"programming",
10+
"easy"
11+
]
12+
13+
print(lst)

stringExample.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#String - Immutable sequence of unicode
2+
#\n - Is a universal new line character. In Windows, no need to add
3+
#\n\r. Just \n will take care
4+
5+
multiline = "This is a python program.\nPython is a dynamic object oriented language"
6+
print(multiline)
7+
8+
escape = "This string has \"double quote\" in it."
9+
print(escape)
10+
11+
#Raw string
12+
path = r'C:\User\python'
13+
print(path)
14+
15+
#String constructor
16+
strValue = str(3000)
17+
print(strValue)
18+
strDouble = str(2.312987e90)
19+
print(strDouble)
20+
21+
#Type function
22+
s= "Python"
23+
print(s[3])
24+
print(type(s[4]))
25+
26+
# Help method
27+
print(help(str))
28+
29+
c = "all lower"
30+
print(c.capitalize()) #String is immutable, creates new string
31+
print(c) # old string unchanged

0 commit comments

Comments
 (0)