Python For Robotics Class Notes
Python For Robotics Class Notes
computer, you need to create a specific type of text, called a source code or a human
readable code, that software can read and then process to the computer in 1s and 0s.
What is python?
Websites built using Python include Google, Youtube, Instagram, Netflix, Uber & much
more.
Example 1 :
name = "shradha"
age = 22
print(name)
print(age)
Example 2 :
name = "shradha"
age = 22
name = "aman"
age = 24
print(name)
print(age)
Example 3 :
first_name = "shradha"
last_name = "khapra"
age = 19
is_adult = True
Taking Input
name = input("What is your name? ")
print("Hello " + name)
print("Welcome to our cool Python class")
new_age = int(old_age) + 2
print(new_age)
Strings
print(name.lower())
print(name)
print(name.find('y'))
print(name.find('Y'))
print(name.find("Stark"))
print(name.find("stark"))
i = 5
i = i + 2
i += 2
i -= 2
i *= 2
Operator Precedence
result = 3 + 5 * 2 # 16 or 13 ?
print(result)
Comments
# This is a comment & useful for people reading your code
# This is another line
Comparison Operators
is_greater = 1 > 5
is_lesser = 1 < 5
# 1 <= 5
# 1 >= 5
is_not_equal = 1 != 5
is_equal = 1 == 5
Logical Operators
# or -> (atleast one is true)
# and -> (both are true)
# not -> (reverses any value)
number = 2
print(number > 3)
print(number < 3)
print(not number > 3)
print(not number < 3)
If statements
age = 13
if operator == "+":
print(first + second)
elif operator == "-":
print(first - second)
elif operator == "*":
print(first * second)
elif operator == "/":
print(first / second)
elif operator == "%":
print(first % second)
else:
print("Invalid Operation")
Range in Python
range() function returns a range object that is a sequence of numbers.
numbers = range(5)
print(numbers)
While Loop
i = 1
while(i <= 5):
print(i)
i = i + 1
i = 1
while(i <= 5):
print(i * "*")
i = i + 1
i = 5
while(i >= 1):
print(i * "*")
i = i - 1
for i in range(5):
print(i * "*")
i = i + 1
Lists
List is a complex type in Python.A list is a type of sequence of data points such as
floats, integers, or strings.
friends = ["amar", "akbar", "anthony"]
print(friends[0])
print(friends[1])
print(friends[-1])
print(friends[-2])
friends[0] = "aman"
print(friends)
marks.insert(0, "math")
marks.insert(1, 99)
print(marks)
print("math" in marks)
print(len(marks)/2)
marks.clear()
print(marks)
i = 0
while i < len(marks):
print(marks[i])
print(marks[i+1])
i = i + 2
Tuples
They are like lists (sequence of objects) but they are immutable i.e. once they have been
defined we cannot change them.
Parenthesis in tuples are optional.
marks = (95, 98, 97, 97)
#marks[0] = 98
print(marks.count(97))
print(marks.index(97))
Sets
Sets are a collection of all unique elements.
Indexing is not supported in sets.
marks = {98, 97, 95, 95}
print(marks)
Dictionary
Dictionary is an unordered collection of Items. Dictionary stores a (key, value) pair.
marks = {"math" : 99, "chemistry" : 98, "physics" : 97}
print(marks)
print(marks["chemistry"])
marks["english"] = 95
print(marks)
marks["math"] = 96
print(marks)
Functions in Python
Function is a piece of code that performs some task. (In a tv remote, each button
performs a functions, so a function is like that button in code)
There are 3 types of functions in Java :
a. In-built functions
# int() str() float() min() range() max()
b. Module functions
Module is a file that contains some functions & variables which can be imported
for use in other files.
Each module should contain some related tasks
Example : math, random, string
import math
print(dir(math))
import random
print(dir(random))
import string
print(dir(string))
sum(1, 2)
sum(1)