Skip to content

Instantly share code, notes, and snippets.

View mayankdawar's full-sized avatar

Mayank Dawar mayankdawar

  • Ludhiana , Chandigarh , Mohali
View GitHub Profile
@mayankdawar
mayankdawar / listOfList.py
Created August 7, 2020 20:47
Below, we’ve provided a list of lists. Use in statements to create variables with Boolean values - see the ActiveCode window for further directions.
L = [[5, 8, 7], ['hello', 'hi', 'hola'], [6.6, 1.54, 3.99], ['small', 'large']]
# Test if 'hola' is in the list L. Save to variable name test1
if 'hola' in L:
test1 = True
else:
test1 = False
# Test if [5, 8, 7] is in the list L. Save to variable name test2
if [5,8,7] in L:
@mayankdawar
mayankdawar / nestedprac.py
Created August 7, 2020 20:45
Below, a list of lists is provided. Use in and not in tests to create variables with Boolean values. See comments for further instructions.
lst = [['apple', 'orange', 'banana'], [5, 6, 7, 8, 9.9, 10], ['green', 'yellow', 'purple', 'red']]
#Test to see if 'yellow' is in the third list of lst. Save to variable ``yellow``
if 'yellow' in lst[2]:
yellow = True
else:
yellow = False
#Test to see if 4 is in the second list of lst. Save to variable ``four``
@mayankdawar
mayankdawar / nested1.py
Created August 7, 2020 20:43
The variable nested contains a nested list. Assign ‘snake’ to the variable output using indexing.
nested = [['dog', 'cat', 'horse'], ['frog', 'turtle', 'snake', 'gecko'], ['hamster', 'gerbil', 'rat', 'ferret']]
output = nested[1][2]
@mayankdawar
mayankdawar / lenList.py
Created June 24, 2020 06:34
# Write a function, length, that takes in a list as the input. If the length of the list is greater than or equal to 5, return “Longer than 5”. If the length is less than 5, return “Less than 5”.
def length(lst):
if len(lst) >= 5:
return("Longer than 5")
else:
return("Less than 5")
@mayankdawar
mayankdawar / formatString.py
Created June 24, 2020 06:32
Write a function called change that takes any string, adds “Nice to meet you!” to the end of the argument given, and returns that new string.
# Write a function called change that takes any string, adds “Nice to meet you!” to the end of the argument given, and returns that new string.
def change(string):
Str = "{}Nice to meet you!".format(string)
return Str
@mayankdawar
mayankdawar / CountLines.py
Created March 16, 2020 10:34
Write code to find out how many lines are in the file emotion_words.txt as shown above. Save this value to the variable num_lines. Do not use the len method.
#Write code to find out how many lines are in the file emotion_words.txt as shown above. Save this value to the variable num_lines. Do not use the len method.
file = open("emotion_word.txt","r")
count = 0
for aline in file.readlines():
count += 1
num_lines = count
@mayankdawar
mayankdawar / ContingLines.py
Created March 16, 2020 10:22
Find the number of lines in the file, travel_plans2.txt, and assign it to the variable num_lines
#Find the number of lines in the file, travel_plans2.txt, and assign it to the variable num_lines.
file = open("travel_plans2.txt","r")
count = file.readlines()
num_lines = len(count)
@mayankdawar
mayankdawar / displayingCharactersOfFile.py
Created March 16, 2020 10:21
Create a string called first_forty that is comprised of the first 40 characters of emotion_words2.txt.
#Create a string called first_forty that is comprised of the first 40 characters of emotion_words2.txt.
file = open("emotion_words2.txt","r")
const = file.read()
first_forty = const[:40]
@mayankdawar
mayankdawar / CountingCharacterInFile.py
Created March 16, 2020 10:07
Using the file school_prompt2.txt, find the number of characters in the file and assign that value to the variable num_char.
#Using the file school_prompt2.txt, find the number of characters in the file and assign that value to the variable num_char.
fil = open("school_prompt2.txt","r")
consts = fil.read()
num_char = len(consts)
fil.close()
@mayankdawar
mayankdawar / lastNLines.py
Created March 11, 2020 05:37
Write a Program to read last n files from a file
# wap to read last n files from a file
f = open("output.txt", "r")
n = int(input("Enter No. of lines:"))
lst = []
for x in f:
l1 = f.readlines()
lst = lst +l1
print(lst[n:])
f.close()