1. Print String and Integer in Python
1 2 3 4 |
print("Hello World") print('Hello World') print(""" Hello World """) print(4+5) |
2. Comment in Python
1 |
#This is a comment |
3. Create Variable in Python
1 2 3 4 5 6 |
name = 'Indocoder' print(name) length = 5 area = length * length print(area) |
4. Updating Variable
1 2 |
x = x+10 -> x+=10 x = x*10 -> x*=10 |
5. Concatenation
1 |
print('My name is'+ name) |
6. Type Conversion
1 2 3 4 5 |
age = 24 print ('My Age is '+str(age)) count = '5' print(int(count)+1) |
7. IF and ELSE Condition
1 2 3 4 5 6 7 |
score = 100 if score == 100: print('Great Job') elif score == 60: print('Not bad') else: print('You bad') |
8. True and False
1 2 |
score = 100 print(score == 100) /*Will return true*/ |
9. AND and OR expression
1 2 3 4 |
if a>10 and a<30: print('a is between 10 and 30') if not z==77: print('z is not 77') |
10. INPUT function
1 2 3 4 |
apple_price = 2 input_count = input('How many apples do you want?:') count=int(input_count) total_price = apple_price * count |
11. LISTS in Python
1 2 3 4 5 6 |
foods = ['pasta','curry','sushi'] print(foods[0]) #Update value (override) foods[1]='pizza' #Append Value foods.append('pizza') |
12. FOR loops
1 2 |
for food in foods: print('I like'+food) |
13. Python DICTIONARY, key-value pair
1 2 3 4 5 6 7 8 9 |
fruits={'apple':'red','banana':'yellow','grape':'purple'} # Will return 'red',don't forget the quote print(fruits['apple']) # Adding value to dictionary fruits['peach'] = 'pink' #Dictionary Loop for fruits_name in fruits: print('The color of '+fruit_name+' is'+fruits[fruit_name]) |
14. WHILE loops
1 2 3 4 |
x =1 while x<=100: print(x) x+=1 |
15. BREAK and CONTINUE during loops
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# Break during loop numbers = [1,2,3,4,5,6] for number in numbers: print(number) if number == 3: break # The opposite is continue numbers = [1,2,3,4,5,6] for number in numbers: if number % 2 == 0: continue print(number) |
16. Python Functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
def greet(): print('Hello') greet() # Functions with argument def greet(name): print("Hello "+name) greet('Johny') # Functions default values def greet(name,message = 'Nice to Meet You'): print("Hello "+name+message) greet('Johny') |
17. Return Value
1 2 3 4 5 |
def add(a,b): return a + b sum = add(1,3) print(sum) |
18. Python Modules
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# This means import from file with name utils.py import utils if utils.validate(player_hand) # Alternative form utils import validate if validate(player_hand) # Python standard library math : modules for complex calculations random : modules for generating random values datetime : modules for handling date and time data randint(1,10) //Random integer between 1 and 10 |
19. Python Classes, Methods, and Properties
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
class MenuItem: def info(self): print('Hi You!') menu_item1 = MenuItem() menu_item1.name = 'Sandwich' //Method is a function inside a class, need first argument to be self class MenuItem: def info(self): # Output in the format '____: $____' print(self.name+': $'+str(self.price)) menu_item2.info() //Special method __init__ , called automatically class MenuItem: # Define the __init__ method def __init__(self): print('An instance of the MenuItem class was created!') from menu_item import MenuItem menu_item1 = MenuItem('Sandwich', 5) menu_item2 = MenuItem('Chocolate Cake', 4) menu_item3 = MenuItem('Coffee', 3) menu_item4 = MenuItem('Orange Juice', 2) # Set the menu_items variable to a list of the MenuItem instances menu_items = [menu_item1, menu_item2, menu_item3, menu_item4] # Create the for loop for menu_item in menu_items: print(menu_item.info()) //menu_item.py class MenuItem: def __init__(self, name, price): self.name = name self.price = price def info(self): return self.name + ': $' + str(self.price) def get_total_price(self, count): total_price = self.price * count return total_price |
20. Class Inheritance
1 2 3 4 5 6 7 8 |
# Inheritance class Food(MenuItem): pass # User super() if both parent and child class has __init__ method def __init__(self, name, price, calorie_count): # Using super() call __init__() from the parent class super().__init__(name,price) |