Python

5 Python Conditional Statement 4 – If..else..elif

If..else..elif If..else..elif : code if person_age < 4: ticket_price = 0elif person_age < 18: ticket_price = 5elif person_age < 65: ticket_price = 10else: ticket_price = 5 Python does not require an else block at the end of an if-elif chain. Sometimes an else block is useful; sometimes you can avoid using.

Advertisements
Python

5 Python Conditional Statements 3 – Condition operators or symbols

Condition symbols or operators Operators evaluation: 1) == Equal to operator - True when matches - False when don't match.CASE == CASE (matches and True in this case)Name = 'Vijay'Name == 'vijay' (False)Age = 18Age == 18 (True) 2) != not equal to operator - True when matches - False when don't match. CASE !=… Continue reading 5 Python Conditional Statements 3 – Condition operators or symbols

Python

5 Python Conditional statements 2 – Simple If..else

simple If..else statement Simple If..else statement: code cities = ['LA', 'Chicago', 'kansas city', 'columbia','Boston'] for city in cities: if city == 'Boston': print(city.upper()) else: print(city.title()) Results: La Chicago Kansas City Columbia BOSTON if statement is an expression that can be evaluated as True or False and is called a conditional test. If a conditional test… Continue reading 5 Python Conditional statements 2 – Simple If..else

Python

5 Python Condition Statements 1 – Intro

Python Conditional Evaluation Programming often involves examining a set of conditions and deciding which action to take based on those conditions. Python’s if statement allows you to examine the current state of a program and respond appropriately to that state. Programming Statements: If...elif...else If..Else statements can be used with:VariablesListsArraysand more....

Python

4 Python List – Pointing to the same List

Pointing to a list Assigning the a variable to a List - Code BOTH VARIABLES POINTING TO THE SAME LIST Any changes made to list appears same in both variables now VAR1 = VAR2 - there is no : used in this case print("Copying list from one to another") my_favorite_foods = ['pizza', 'rice', 'cake'] friends_favorite_foods… Continue reading 4 Python List – Pointing to the same List

Python

4 Python List – Appending to a list

Appending Appending: Code #APPENDING TO LIST print("Copying list from one to another") my_favorite_foods = ['pizza', 'rice', 'cake'] friends_favorite_foods = my_favorite_foods[:] #APPEND print("Append new food items") my_favorite_foods.append('fruits') friends_favorite_foods.append('ice cream') Print print("My favorite foods are:") print(my_favorite_foods) print("\nMy friend's favorite foods are:") print(friends_favorite_foods) Results: Append new food items My favorite foods are: ['pizza', 'rice', 'cake', 'fruits', 'ice cream']… Continue reading 4 Python List – Appending to a list

Python

4 Python Lists – Copying Lists

Copying List COPYING LIST code print("Copying list from one to another") my_favorite_foods = ['pizza', 'rice', 'cake'] friends_favorite_foods = my_favorite_foods[:] print("My favorite foods are:") print(my_favorite_foods) print("\nMy friend's favorite foods are:") print(friends_favorite_foods) Results: Copying list from on to another My favorite foods are: ['pizza', 'rice', 'cake'] My friend's favorite foods are: ['pizza', 'rice', 'cake']

Python

4 Python List – Slicing the List

SLICING a LIST code players = ['Vijay', 'Mohan', 'Vasu', 'Sai', 'Chandika','Shreya'] print("Here are the three players from 2ND PLAYER ONWARDS on my team:") print(players[1:4]) players = ['Vijay', 'Mohan', 'Vasu', 'Sai', 'Chandika','Shreya'] print("Here are the FIRST THREE players on my team:") print(players[0:3]) players = ['Vijay', 'Mohan', 'Vasu', 'Sai', 'Chandika','Shreya'] print("Here are the FIRST FOUR players on… Continue reading 4 Python List – Slicing the List

Python

4 Python LIST – Looping through a slice

Python LIST Looping Through a SLICE print("Looping through SLICE code") players = ['Vijay', 'Mohan', 'Vasu', 'Sai', 'Chandika','Shreya'] print("Here are the first three players on my team:") for player in players[:3]: print(player.title()) Results Looping through SLICE code Here are the first three players on my team: Vijay Mohan Vasu