Skip to content

Instantly share code, notes, and snippets.

View slingam00's full-sized avatar

Sanjeev Lingam-Nattamai slingam00

View GitHub Profile
# Integer Variable
intVariable = 1
# Float Variable
floatVariable = 1.5
# String Variable
stringVariable = "hello world"
# Boolean Variable
intVariable = 1
print(intVariable) # will print out the value intVariable is assigned to which is 1
# If Statements
if True:
print("The If statement is True")
if 2 > 1:
print("2 is greater than 1")
# Else Statements
if 2 < 1:
myList = [1, 2, 3, 4, 5]
# List Appending
myList.append(6)
print(myList) # Result: [1, 2, 3, 4, 5, 6]
# Removing an element
myList.remove(2)
print(myList) # Result: [1, 3, 4, 5, 6]
myTuple1 = (1, 2, 3, 4, 5)
myTuple2 = ('a', 1, 'b', 2, 'c', 3')
# Indexing
print(myTuple1[2]) # Result is 3
# Range Indexing
print(myTuple1[2:4]) # Result is (3, 4)
mySet = {'a', 'b', 'c'}
# Adding to the set
mySet.add('d')
print(mySet) # Sets are unordered so the resulting Set varies
# An example of a result would be {'a', 'd', 'b', 'c'}
# Removing from the set
mySet.remove('c')
myDictionary = {
'a': 1,
'b': 2,
'c': 3
}
# Keys are: 'a', 'b', 'c'
# Values are: 1, 2, 3
# Can get the value of a specific key in 2 ways
myList = [1, 2, 3, 4, 5]
# Iterating through a list
for i in myList:
print(i) # The elements in myList will each get printed on a new line
for i in range(len(myList)):
print(myList[i]) # Same thing, but the elements are getting index and then printed out
# Iterating through a string
incrementer = 1
while incrementer < 6:
print(incrementer)
incrementer = incrementer + 1 # re-assigning incrementer to one value higher
# while loop breaks when incrementer equals 6
# the while loop runs 5 times and
# 1, 2, 3, 4, and 5 are printed on 5 lines
def hello():
return("Hello")
print(hello()) # printing the call of the function which returns "Hello"
def name(firstName, lastName):
return("Hello, my name is " + firstName + " " + lastName)
print(name("John", "Doe")) # name function gets called