Epython Lab
#Python #DataScience #String There are two functions that let you determine the positions of (the first occurrence of) a substring inside a string. Example:- text = "Data Science" Can you determine the position of "Sci" using both of the functions? …
#Solution
1. Using index() function we can determine the position of the first character of the substring in the string example: text.index("Sci") is 5.
2. The same thing we can use find() function
Example: text.find("Sci") is 5.
Note: the difference between these functions in how they deal with the situation when the substring is not contained in the string. For the index() function an error is generated but instead the find() function returns -1.
Example: text.find("s") # -1
text.index("s) # return error
Because small "s" is not found in the string "Data Science"
#DataScience #String #Python
#Prevent #COVID19
1. Using index() function we can determine the position of the first character of the substring in the string example: text.index("Sci") is 5.
2. The same thing we can use find() function
Example: text.find("Sci") is 5.
Note: the difference between these functions in how they deal with the situation when the substring is not contained in the string. For the index() function an error is generated but instead the find() function returns -1.
Example: text.find("s") # -1
text.index("s) # return error
Because small "s" is not found in the string "Data Science"
#DataScience #String #Python
#Prevent #COVID19
#Solution for #Q1 by @oneyedking
split() function used to split string to substrings
Example :
Str = "coding with python"
Str.split(' ')
Result : "coding", "with", "python"
By @met_asploit
There are built in func
split and join
while split is used to separate a string and create a list , join is used to combine list and create a string
>>> a = ['a' , 'b' , 'c' , 'd']
>>>' '.join(a)
'a b c'
>>>b = 'hello how are you'
>>>b.split(' ')
['hello' , 'how' , 'are' , 'you']
split() function used to split string to substrings
Example :
Str = "coding with python"
Str.split(' ')
Result : "coding", "with", "python"
By @met_asploit
There are built in func
split and join
while split is used to separate a string and create a list , join is used to combine list and create a string
>>> a = ['a' , 'b' , 'c' , 'd']
>>>' '.join(a)
'a b c'
>>>b = 'hello how are you'
>>>b.split(' ')
['hello' , 'how' , 'are' , 'you']
#Solution for #Q2
#DataScience #DataCollection
#Collectiondatatype
We will continue discussing on other collection types in next time.
Stay tuned
✅Apply health advises.
#QuarantineYourself
#DataScience #DataCollection
#Collectiondatatype
We will continue discussing on other collection types in next time.
Stay tuned
✅Apply health advises.
#QuarantineYourself
#Solution for #Assignment by @MINtaa911
You can forward your feedback via @pyDiscussion
#Guess the number
from random import randint
def guess_number():
num = randint(0,20)
guess = int(input("Enter your guess number: "))
while(num != guess):
if(guess > num):
print("Your guess is too high")
elif(guess < num):
print("your guess is too low")
else:
break
guess = int(input("Enter your guess number: "))
print("you win")
guess_number()
#LearnDataScience #LearnPython #StayHome #PreventCOVId19
You can forward your feedback via @pyDiscussion
#Guess the number
from random import randint
def guess_number():
num = randint(0,20)
guess = int(input("Enter your guess number: "))
while(num != guess):
if(guess > num):
print("Your guess is too high")
elif(guess < num):
print("your guess is too low")
else:
break
guess = int(input("Enter your guess number: "))
print("you win")
guess_number()
#LearnDataScience #LearnPython #StayHome #PreventCOVId19
Another #Solution for #Assignment by @Amalright
You can forward your feedback via @pyDiscussion
from random import*
Def fun_game(number_chosen):
data=randrange(0,20)
if(nuber_chosen> data):
print("too much")
elif(nuber_chosen < data):
print("too low")
else:
print("correct answer :")
J = 1
While(j !=0)
choice =int(input("Enter 1 to continue"))
if(choice ==1):
choice =int(input("Enter your try"))
fun_game(choice)
else:
j =0
#StayHome #PreventCOVId19 #LearnPython #LearnDataScience #LearnRandomNumber
You can forward your feedback via @pyDiscussion
from random import*
Def fun_game(number_chosen):
data=randrange(0,20)
if(nuber_chosen> data):
print("too much")
elif(nuber_chosen < data):
print("too low")
else:
print("correct answer :")
J = 1
While(j !=0)
choice =int(input("Enter 1 to continue"))
if(choice ==1):
choice =int(input("Enter your try"))
fun_game(choice)
else:
j =0
#StayHome #PreventCOVId19 #LearnPython #LearnDataScience #LearnRandomNumber
#Question_by_user
Q: "Can somebody help me in putting list in CSV file in Python"
#Solution
# first create a list with items you would like to save a csv file
big_list = [{'name': 'Fredrick Stein', 'userid': 6712359021, 'is_admin': False},
{'name': 'Wiltmore Denis', 'userid': 2525942, 'is_admin': False},
{'name': 'Greely Plonk', 'userid': 15890235, 'is_admin': False},
{'name': 'Dendris Stulo', 'userid': 572189563, 'is_admin': True}]
# import csv module
import csv
# create afile with open function with a temporary file name output.csv
with open('output.csv', 'w') as output_csv:
# create a list of headers
fields = ['name', 'userid', 'is_admin']
# write the output using dictionary writter function
output_writer = csv.DictWriter(output_csv, fieldnames=fields)
# write the headers
output_writer.writeheader()
# create for loop which iterate each list and write a row
for item in big_list:
output_writer.writerow(item)
Q: "Can somebody help me in putting list in CSV file in Python"
#Solution
# first create a list with items you would like to save a csv file
big_list = [{'name': 'Fredrick Stein', 'userid': 6712359021, 'is_admin': False},
{'name': 'Wiltmore Denis', 'userid': 2525942, 'is_admin': False},
{'name': 'Greely Plonk', 'userid': 15890235, 'is_admin': False},
{'name': 'Dendris Stulo', 'userid': 572189563, 'is_admin': True}]
# import csv module
import csv
# create afile with open function with a temporary file name output.csv
with open('output.csv', 'w') as output_csv:
# create a list of headers
fields = ['name', 'userid', 'is_admin']
# write the output using dictionary writter function
output_writer = csv.DictWriter(output_csv, fieldnames=fields)
# write the headers
output_writer.writeheader()
# create for loop which iterate each list and write a row
for item in big_list:
output_writer.writerow(item)
👍3
Epython Lab
#CODE_CHALLENGE #PYTHON_LIST #LOOPS #FUNCTIONS 1. Write a function called delete_starting_evens() that has a parameter named lst. The function should remove elements from the front of lst until the front of the list is not even. The function should then…
#SOLUTION
Many of you are tried your best solution for the challenge. We thank you so much for your contribution.
We also thanks to someone who shared the info to others.
based on the instruction this is the best solution for the challenge
#Write your function here
def delete_starting_evens(lst):
# then check the condition here
while (len(lst) > 0 and lst[0] % 2 == 0):
# then slice the list
lst = lst[1:]
return lst
# Call the function and print the result
print(delete_starting_evens([4, 8, 10, 11, 12, 15]))
print(delete_starting_evens([4, 8, 10]))
# output
[11, 12, 15]
[]
Many of you are tried your best solution for the challenge. We thank you so much for your contribution.
We also thanks to someone who shared the info to others.
based on the instruction this is the best solution for the challenge
#Write your function here
def delete_starting_evens(lst):
# then check the condition here
while (len(lst) > 0 and lst[0] % 2 == 0):
# then slice the list
lst = lst[1:]
return lst
# Call the function and print the result
print(delete_starting_evens([4, 8, 10, 11, 12, 15]))
print(delete_starting_evens([4, 8, 10]))
# output
[11, 12, 15]
[]
Epython Lab
#CODE_CHALLENGE_3 #LIST #SWAP_TWO_NUMBERS #Q: The next iteration of Asibeh Tenager will feature an extra-infuriating new item, the Purple Shell. When used, it warps the last place racer into first place and the first place racer into last place. Write a…
#Solution for #CODE_CHALLENGE_3
# Given a list of racers, set the first place racer (at the front of the list)
# Use swap mechanism
def purple_shell(lst):
# swap list items
temp = lst[0]
lst[0] = lst[2]
lst[2] = temp
return lst
# code driver
racers = ['Asibeh', 'Naol', 'Obang']
purple_shell(racers)
#Output: ['Obang', 'Naol', 'Asibeh']
# Given a list of racers, set the first place racer (at the front of the list)
# Use swap mechanism
def purple_shell(lst):
# swap list items
temp = lst[0]
lst[0] = lst[2]
lst[2] = temp
return lst
# code driver
racers = ['Asibeh', 'Naol', 'Obang']
purple_shell(racers)
#Output: ['Obang', 'Naol', 'Asibeh']
Epython Lab
#Question_by_user Q: "Can somebody help me in putting list in CSV file in Python" #Solution # first create a list with items you would like to save a csv file big_list = [{'name': 'Fredrick Stein', 'userid': 6712359021, 'is_admin': False}, {'name':…
In case this may be help you. This is the solution for questioned by our member. You can also ask your question in well defined manner. Check this for question answer
https://t.me/epythonlab/234
https://t.me/epythonlab/234
Telegram
EPYTHON LAB
#Question_by_user
Q: "Can somebody help me in putting list in CSV file in Python"
#Solution
# first create a list with items you would like to save a csv file
big_list = [{'name': 'Fredrick Stein', 'userid': 6712359021, 'is_admin': False},
…
Q: "Can somebody help me in putting list in CSV file in Python"
#Solution
# first create a list with items you would like to save a csv file
big_list = [{'name': 'Fredrick Stein', 'userid': 6712359021, 'is_admin': False},
…