Skip to content

Instantly share code, notes, and snippets.

@afrinjamanbd
Created February 5, 2021 04:41
Show Gist options
  • Save afrinjamanbd/2f92bed4952b1c44708bfc856f8fdfca to your computer and use it in GitHub Desktop.
Save afrinjamanbd/2f92bed4952b1c44708bfc856f8fdfca to your computer and use it in GitHub Desktop.
Python Cheat Sheet
course = 'my name is afrin'
print(f'hi {course[:7]} and {course[3:-4]} or {course[5:7]}')
print(len(course[2:5]))
print(course.upper())
print(course.lower())
print(course.find('n'))
print(course.find('y name'))
print(course.find('no'))
print(course.replace('my', 'her'))
if 'my' in course:
print("my")
if 'afrin' in course or 'any name' in course:
print('or checked')
if ' ' in course and 'my' in course:
print("and checked")
# and not, or not, not
xi = input()
x = float(xi)
print(round(x))
print(abs(x))
y = math.ceil(x)
print(y)
y = math.floor(x)
print(y)
#list methods = append, insert, index, remove, reverse, clear, copy, extend, in, sort,pop
numbers = [2, 4535, 65, 34, 567, 5675]
numbers.append(234)
numbers.insert(2, 3333)
print(numbers)
print(numbers.count(2))
numbers.remove(65)
print(numbers)
numbers.sort()
print(numbers)
numbers.reverse()
print(numbers)
mylist = ['asian', 'germany', 'african']
numbers.extend(mylist)
print(numbers)
numbers.extend('fuck')
print(numbers)
numbers.pop(8) #provide index number
print(numbers)
# The problem with copying lists with '=' is that if you modify new_list,
# old_list is also modified.
# It is because the new list is referencing or pointing to the same old_list object
old_list = [1, 2, 3]
new_list = old_list
# add an element to list
new_list.append('a')
print('New List:', new_list)
print('Old List:', old_list)
yourlist = numbers.copy()
numbers.clear()
print(yourlist)
print(numbers)
print('asian' in yourlist)
print(yourlist.index('asian'))
#tuple unpacking or list unpacking
indices = (3, 5, 8) #tuple
a, b, c = indices
print(a)
items = [1, 4, 6, 8]
p, q, r, s = items
print(r)
#Dictionaries
human = {
'name': 'gorge',
30: 'you',
True: 50
}
print(human[True])
print(human.get('last name', 'Not found....'))
human['last name'] = 'geller'
print(human)
uinput = input()
word = uinput.split(" ")
print(word)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment