PEMDAS :parentheses//exponent //multiply // addition // subtraction // division
Conversion number to String
str(year)
print()
print("i m here");
str = "i m here";
print(str);
print(str," will be there");
print("Hello's "); // use double quotes when using num
title ="\n\t roo rooo roo roo roo !"
print("Song: ",title);
print(len(str));
will print length of string
Slice Operation
PYTHON :
PYTHON[2:5]
PYTHON[:2]
PYTHON[2:]
Integer Division
result = number1//number2
https://www.python.org/dev/peps/pep-0008/
-----------------------------------------------------------------
rain_speed =7;
if rain_speed < 5:
print("Just a Scotch mist")
else:
print("It's a real Cow-quaker out there")
-----------------------------------------------------------------
Conditional Operators :
and = &&
or = ||
equals = "=="
How to take user input ?
day = input('please enter your name');
print('you entered',day);
convert user input to number
day = int(input('please enter your name'));
print('you entered',day);
elif :
number = 5 if number < 5: print('Aloha') elif number <= 10: print('Hola') else: print('Konichiwa')
NOTE: THERE ARE NO SEMICOLONS IN PYTHON
computer_choice = 'python'
user_choice = input("Enter rock, paper, or python:\n")
if computer_choice == user_choice:
print('TIE')
else :
print("You lose :( Computer wins :D")
https://www.codeschool.com/account/courses/try-python
Performances [LIST]
ADDITION
performances = ['Ventriloquism','Amazing Acrobatics']
performances.append('Snake Charmer')
print(performances)
SUBTRACTION
performances = ['Ventriloquism', 'Amazing Acrobatics', 'Snake Charmer', 'Enchanted Elephants', 'Bearded Lady', 'Tiniest Man']
performances.remove('Bearded Lady')
['Ventriloquism', 'Amazing Acrobatics', 'Snake Charmer', 'Enchanted Elephants', 'Bearded Lady', 'Tiniest Man']
performances.remove('Tiniest Man')
print(performances)
Dictionary:
performances = {'Ventriloquism':'9:00am', 'Snake Charmer': '12:00pm'}
Adding to dictionary
performances['Amazing Acrobatics'] = '2:00pm'
performances['Enchanted Elephants'] = '5:00pm'
print(performances)
Removing to dictionary
performances.remove('Ventriloquism') --incorrect
del performaces['Ventriloquism']
Dictionary with performances
performances = {'Ventriloquism':'9:00am',
'Snake Charmer': '12:00pm',
'Amazing Acrobatics': '2:00pm',
'Bearded Lady':'5:00pm'}
showtime = performances.get('Bearded Lady');
if showtime == None:
print("Performance doesn't exist")
else:
print("The time of the Bearded Lady show is ",showtime)
Comparing and Combining
Comparing list
list_a = ['python', 'bison', 'lion'] list_b = ['python', 'lion', 'bison'] print(list_a == list_b)
Comparing dictionaries
dict_a = {'python':'reptile', 'bison':'mammal', 'lion':'mammal'} dict_b = {'python':'reptile', 'lion':'mammal', 'bison':'mammal'} print(dict_a == dict_b)
List Of List
performances = {'weekdays': ['Bearded Lady', 'Tiniest Man', 'Ventriloquist Vinnie'], 'saturday': ['Amazing Acrobatics', 'Enchanted Elephants'], 'sunday': ['Snake Charmer', 'Amazing Acrobatics']}
performances['weekdays'][2]
LEVEL 2 :Looping
Package Random
For loop with range
----------------------------------------------------------------------------------------------------------------------
he built-in function
range()
to generate numbers from 0 to 19.my_list = range(20)
import random
for i in range(5):
no =random.randint(1,53)
print(no)
Printing prices
for items in menu_prices
print(item) ------------------------> prints item names
Printing prices
for items in menu_prices.items()
print(item) ------------------------> prints prices
Printing key & value
for name,price in menu_prices.items()
print(name,'$ :', price ) ------------------------> prints prices
Printing key & value with space seperator
for name,price in menu_prices.items()
print(name,'$ :', price, sep=' ' ) ------------------------> prints prices
Printing key & value with space seperator & formatting
for name,price in menu_prices.items()
print(name, '$ :', format(price, 2.f), sep=' ' ) ------------------------> prints prices
performances = {'Ventriloquism':'9:00am',
'Snake Charmer': '12:00pm',
'Amazing Acrobatics': '2:00pm',
'Enchanted Elephants':'5:00pm'}
for show,showtime in performances.items():
print(show,": ",showtime,sep="")
The while loop
import random
num = random.randint(1,10)
guess = int(input('Guess a number between 1 and 10'))
times = 1
while guess != num:
times =times+1
if times==3:
break
guess = int(input('Guess again'))
if guess==num:
print('You win!')
else:
print('You lose! The number was ',num)
---------------------------------------------------------------------------------------------------------------------
LEVEL 3 : FUNCTIONS
import random
def lotto_numbers():
# code in the function goes here
lotto_nums =[]
for i in range(5):
lotto_nums.append(random.randint(1, 53))
return lotto_nums
numbers = lotto_numbers()
print(numbers)
---------------------------------------------------------------------------------------------------------------------
main
import random
def lotto_numbers():
lotto_nums = []
for i in range(5):
lotto_nums.append(random.randint(1, 53))
return lotto_nums
def main():
# code in the main function goes here
numbers = lotto_numbers()
print(numbers)
main()
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
import random
def guessing_game():
num = random.randint(1, 10)
guess = int(input('Guess a number between 1 and 10'))
times = 1
while guess != num:
guess = int(input('Guess again'))
times += 1
if times == 3:
break
if guess == num:
print('You win!')
else:
print('You lose! The number was', num)
def lotto_numbers():
lotto_nums = []
for i in range(5):
lotto_nums.append(random.randint(1, 53))
return lotto_nums
def main():
guessing_game()
numbers = lotto_numbers()
print(numbers)
main()
---------------------------------------------------------------------------------------------------------------------
import random
def guessing_game():
num = random.randint(1, 10)
guess = int(input('Guess a number between 1 and 10'))
times = 1
while guess != num:
guess = int(input('Guess again'))
times += 1
if times == 3:
break
if guess == num:
print('You win!')
else:
print('You lose! The number was', num)
def lotto_numbers():
lotto_nums = []
for i in range(5):
lotto_nums.append(random.randint(1, 53))
return lotto_nums
def main():
answer =input('Do you want to get lottery numbers (1) or play the game (2) or quit (Q)?')
if(answer == '1'):
numbers = lotto_numbers()
print(numbers)
elif(answer == '2'):
guessing_game()
else:
print('Toodles!')
main()
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
LEVEL 3 : READING AND WRITING TO FILES
---------------------------------------------------------------------------------------------------------------------
LEVEL 3 : READING TO FILES
performances = {'Ventriloquism': '9:00am',
'Snake Charmer': '12:00pm',
'Amazing Acrobatics': '2:00pm',
'Enchanted Elephants': '5:00pm'}
#schedule_file = open('schedule.txt','a')
schedule_file = open('schedule.txt','w')
for key,val in performances.items():
schedule_file.write(key+' - '+val+'\n')
schedule_file.close()
schedule_file = open('schedule.txt', 'r')
for line in schedule_file:
(show,time) =line.split(' - ')
print(show,time)
schedule_file.close()
performances = {}
schedule_file = open('schedule.txt', 'r')
for line in schedule_file:
(show, time) = line.split(' - ')
print(show, time)
performances[show] = time.strip()
schedule_file.close()
print(performances)
---------------------------------------------------------------------------------------------------------------------
EXCEPTIONS
---------------------------------------------------------------------------------------------------------------------
LEVEL 4 : MODULES
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
import requests
url = "http://api.openweathermap.org/data/2.5/weather?q=Orlando,fl&units=imperial&appid=2de143494c0b295cca9337e1e96b00e0"
request = requests.get(url)
weather_json = request.json()
weather_main = weather_json['main']
print(weather_json)
temp = weather_main['temp']
print("The Circus's current temperature is ",temp)
---------------------------------------------------------------------------------------------------------------------
Using Modules to access functions
-----------
-----------------------------------------------------------------------------------------------------------
Additional Info:
Python has five standard data types −
Numbers-----------------------------------------------------------------------------------------------------------
Additional Info:
Python has five standard data types −
String
List == list
Tuple == collection
Dictionary == hashmap
How to clear screen in python
----------------------------------------------------------------------------------------------------------------------
def cls(): print ("\n" * 100);
cls()
or simplyExercise :print ("\n" * 100);
Method | Description |
---|---|
Python Tuple count() | returns occurrences of element in a tuple |
Python Tuple index() | returns smallest index of element in tuple |
Python any() | Checks if any Element of an Iterable is True |
Python all() | returns true when all elements in iterable is true |
Python ascii() | Returns String Containing Printable Representation |
Python bool() | Coverts a Value to Boolean |
Python enumerate() | Returns an Enumerate Object |
Python filter() | constructs iterator from elements which are true |
Python iter() | returns iterator for an object |
Python len() | Returns Length of an Object |
Python max() | returns largest element |
Python min() | returns smallest element |
Python map() | Applies Function and Returns a List |
Python reversed() | returns reversed iterator of a sequence |
Python slice() | creates a slice object specified by range() |
Python sorted() | returns sorted list from a given iterable |
Python sum() | Add items of an Iterable |
Python tuple() Function | Creates a Tuple |
Python zip() | Returns an Iterator of Tuples |
----------------------------------------------------------------------------------------------------------------------
Goals 2018 :
https://www.programiz.com/python-programming/methods/built-in
https://www.programiz.com/python-programming/methods/string/capitalize
https://see.stanford.edu/Course/CS229/47
Machine Learning : NLP & Python
Great Readings
https://www.analyticsvidhya.com/blog/2017/12/introduction-to-altair-a-declarative-visualization-in-python/
How to schedule python scripts
https://www.youtube.com/watch?v=3l9mYAMzpko
https://www.youtube.com/watch?v=iyv1iySErc0
Running python script as a service
https://www.youtube.com/watch?v=aXu6T4XfZGU
https://www.youtube.com/watch?v=lOIJIk_maO4
----------------------------------------------------------------------------------------------------------------------
https://www.programiz.com/python-programming/methods/string/capitalize
https://see.stanford.edu/Course/CS229/47
Machine Learning : NLP & Python
Great Readings
https://www.analyticsvidhya.com/blog/2017/12/introduction-to-altair-a-declarative-visualization-in-python/
How to schedule python scripts
https://www.youtube.com/watch?v=3l9mYAMzpko
https://www.youtube.com/watch?v=iyv1iySErc0
Running python script as a service
https://www.youtube.com/watch?v=aXu6T4XfZGU
Convert PY to EXE
https://www.youtube.com/watch?v=lOIJIk_maO4
----------------------------------------------------------------------------------------------------------------------
No comments:
Post a Comment