Posts

Showing posts with the label Python

[Python] Guessing game with while loop

 This time we're going to make Guessing game.. with While Loop that we learned last time. print ( "Food name" ) secret_word = "pasta" guess = "" I made secret_word while guess != secret_word :     guess = input ( "Enter guess: " )     print ( "Try more~" ) print ( "You win!" ) != means "not equal" so if the guess is not equal to secret_word, it's going to continue to loop through  Let's play Food name Enter guess : pizza Try more~ Enter guess : sushi Try more~ and when the guess is right  You win! You can set up "limit" to make it more fun guess_count = 0 guess_limit = 3 out_of_guesses = False I make count, limit, and out of guess while guess != secret_word and not ( out_of_guesses ):     if guess_count < guess_limit :         guess = input ( "Enter guess: " )         guess_count += 1         print ( "try more~" )     else :         ou...

[Python] While Loop !

 While loop is basically a structure of python, which allows us to loop through and execute a block of codes multiple times.  Let's make while loop  i = 1 while i <= 10 : # while + condition:     print ( i )     i += 1 # i = i + 1 print ( "done with loop" ) while i <= 10 : it means while 1,2,3,4,5,6,7,8,9,10, executing the codes   i += 1 it means i = i +1 , short version.  In python,  "=" means  "include" Let's run  1 2 3 4 5 6 7 8 9 10 done with loop so basically,  i = 1 while i <= 10 :     print ( i )     i += 1 print ( "done with loop" ) it started with i ( it is 1 ) -> 1 is less than 10 -> print 1 -> i + 1 -> start with 2 -> 2 is less than 10 -> print 2 > 2+1 -> start with 3 ................................. untill 11.  because 11 is greater than 10 and stop looping and print "done with loop"  Quiz) while i <= 100 :   what if...

[Python] Dictionary

This Chapter, we're going to make basic dictionary.  mondict = {     "1" : "Janunary" ,     "2" : "February" ,     "3" : "March" ,     "4" : "April" } whenever you make dictionary in python, you're going to use open and close curly brackets "{ }" Let's run  print ( mondict [ "2" ]) you get "February"  you can also use "get" function print ( mondict . get ( "2" )) you can specify default value that I wanna use if the key is not found by using get function. so when you run this print ( mondict . get ( "6" )) 6 is not on the list, you're going to get "none"  you can also pass the default value to print out  print ( mondict . get ( "6" , "invalid value" )) and you're going to get "invalid value"

[Python] Creating Better Calculator !

 Last time we made the basic calculator, now we're going to make a better one with if statement. Let me make these; num1 = float ( input ( "Enter first number:" )) op = input ( "Enter operator(+ - / *):" ) num2 = float ( input ( "Enter second number:" )) so the user is going to be asked to put the numbers and operator themselves.  and make the operation with if statements here  if op == "+" :     print ( num1 + num2 ) elif op == "-" :     print ( num1 - num2 ) elif op == "/" :     print ( num1 / num2 ) elif op == "*" :     print ( num1 * num2 ) else :     print ( "Invalid" ) I made +, -, /, *  you can make more.. whatever you want and Let's Run Enter first number : 5.2 Enter operator(+ - / *): * Enter second number: 55.1 286.52000000000004

[Python] Comparison with If statement

This time you can make comparison function; with return & If statement def num ( n1 , n2 , n3 ):     if n1 >= n2 and n1 >= n3 :         return n1     elif n2 >= n1 and n1 >= n3 :         return n2     else :         return n3 print ( num ( 13 , 40 , 5 )) You're going to get "40" in python,  equal sign is "==", not equal is "!=" != ==  

[Python] If statement

 In this chapter we're going to learn if statement, this function is very useful. I'm going to create Boolean variable(True or False) that's going to store whether or not the user is male is_male = True if is_male :     print ( "You are a man" ) Let's say is_male = True; means  male is True, The other is False  so if it's the True, python is going to print "You are a man" and if it's not male, python prints nothing If you want to print something even if it's not true, you can make like this; is_male = True if is_male :     print ( "You are a man" ) else :     print ( "you are not a man" ) then it will print "you are not a man" instead of nothing printed. let me make it more complex is_male = False is_tall = True if is_male or is_tall :     print ( "You are a man or tall or both" ) else :     print ( "you neither man or tall" ) You are a man or tall or both now we made two Boolea...

[Python] Return Statement

Python function is just collection of python codes that perform specific tasks. So we just call python function when we want perform that tasks; However, sometimes when we call the function we're actually  going to get the information back from that function.  "Return" keyword, which allows python to return the information from the function.  Let's make this function;  you can actually make your own function with "def"  def cube ( num ):     num * num * num and run..  print ( cube ( 3 )) but we got "none"  We expected it would cube the numbers .. but NOT ! Now, Let's do this def cube ( num ):     return ( num * num * num ) Python is going to return whatever value we put inside. so when I call the cube function, and give it "3" python is going to execute the function and return the values.  print ( cube ( 3 )) 27 we got the right number ! one more ! you can do like this too  def cube ( num ):     return ( num *...

[Python] Don't worry about Dinner menu; (Phyton Random Choice for 10seconds)

 In this chapter, we're going to make Random Choice for Dinner menu for 10 secs; OK!  import random import random first ..  Dinner_List = [ "Pizza" , "Pasta" , "Sushi" , "Curry" , "Chicken" , "Hamburger" ] make your own menu! print ( random . choice ( Dinner_List )) and run...! Sushi I got sushi tonight :)

[Python] Functions :)

 In python, you can create your own function.  First you need to define the function; def  def give python specific names.  def go_home (): # two or more function, you use underscore_     print ( "I wanna get off" ) we need function name (go_home) and parenthesis () and :  To be considered inside of the function, it has to be indented      print ( "I wanna get off" ) Now we call the function.. ; go_home () I wanna get off One more I want to tell you.. ; def go_home ( name ):     print ( "Get home, " + name ) go_home ( "John" ) go_home ( "Mary" ) This time I added something inside of parenthesis, which means whenever you use go_home function, you have to give the names.  Let's Run. Get home, John Get home, Mary You can add more parameters  def go_home ( name , age ):     print ( "Get home, " + name + ", your number is " + age ) go_home ( "John" , "30" ) go_home...

[Python] Tuples?

Tuples are  used to store multiple items in a single variable . Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage. A tuple is a collection which is ordered and unchangeable . whenever create tuples, we're using open and close   parenthesis () coordinates = ( 2 , 5 ) print ( coordinates [ 1 ]) You're going to get "5"  What's the difference between List and Tuple ? List                     vs                  Tuple   [ ]                                                 ( )    Changeable                        Unc hangeable  Python Collections (Arrays) There ar...

[Python] How to make lists ???????

HOW to make Lists in Python ? It is actually easy to make lists in Python, Let's Start patients = [ "John" , "Donald" , "Kevin" ] when you make the lists, you're going to use square brackets [ ] for the values of the Lists. Let's say, we'd like to print "John" on the screen, what would you do?  Like I mentioned earlier on the other chapter, In Python index number starts from Zero.         [ "John" , "Donald" , "Kevin" ] Index :   0         1         2 so John is 0, Donald is 1, Kevin is 2 Now we'd like to take out John to the screen, what are you going to do?  right,  print(patients[0]) Then we get "John" Let's make more values  patients = [ "John" , "Donald" , "Kevin" , "Tony" , "Alice" ] print ( patients [ 1: ]) 1: means you're going to bring from index 1 to the end.  So you get  "Donald" , "Kevin" ...

[Python] Mad Libs, Basic Game

Image
 In this Chapter, we're going to make Mad Libs Game, which is making some words and getting the sentence with the words.  Let's make some random factors country = input ( "Enter a contry: " ) job = input ( "Enter a job: " ) celebrity = input ( "Enter a celebrity: " ) and then..  print ( "I met a girl in " + country ) print ( "she is a " + job ) print ( "she looks like " + celebrity ) Let's Run ! Enter a contry : Canada Enter a job : teacher Enter a celebrity : Ariana And.. Enter !  I met a girl in Canada she is a teacher she looks like Ariana Oh ! This is my wish 😋

[Python] Build a Basic Calculator !

Hello ~ In this chapter, we're going to make a basic calculator by Python !  in Python, we can calculate numbers; for instance..  print ( 5 + 4 ) you'll get "9"  print ( 6 * 4 ) you get 24  Now ! Let's make a calculator;  num1 = input ( "Enter a number: " ) num2 = input ( "Enter another number:" ) result = num1 + num2 print ( result ) --------------------------------------------------------------- Enter a number : 10 Enter another number : 2 result : 102 What !?  I expected 10 + 2 = 12, but it got 102 as a result.  Why it happened is .. Python accepts num1+num2 as a string.!  What we have to do is let Python know those are numbers by putting  int  (integer) num1 = input ( "Enter a number: " ) num2 = input ( "Enter another number:" ) result = int ( num1 ) + int ( num2 ) print ( result ) and Put the same numbers  Enter a number : 10 Enter another number : 2 result : 12 we got the number we want :)  However, you're g...

What is... Python ? Easy to learn !

When you make conversation, you use a language like English.  In the computer, you can talk to your computer in programming language to make them work !  Python is the one of the languages where you can talk to the computer.  We're going to learn how to use python 😊 First, just download python. Search on google "Python"  Second, you can set up "Visual studio code" or "Pycharm"  to run the Python easily  1.  Print  if you just type words like "hello", computer receive the word but not showing on the screen.  To make them show on the screen ,  we say, print("hello") print("hello") 2. Variables and Data Types In Python, you can designate valuables like "character's name or age"  let's say you want to make 100 sentences and they're all have same variable. you may not want to put the variable at every single sentences.  In Python, It makes things easier. --------------------  Let's say,, character...