Knowledge in Python

Removing Items

Removing ItemsThere are several methods to remove items from a dictionary:ExampleThe pop() method removes the item with the specified key name: thisdict = {   "brand": "Ford",   "model": "Mustang",   "year": 1964 }thisdict.pop("model") print(thisdict) ExampleThe popitem() method removes the last inserted item (in versions before 3.7, a random item is removed instead): thisdict = {   "brand": "Ford",   "model": "Mustang",   "year": 1964 }thisdict.popitem() print(thisdict) ExampleThe del keyword removes the item with the specified key name: thisdict = {   "brand": "Ford",   "model": "Mustang",   "year": 1964 }del thisdict["model"]print(thisdict) ExampleThe del keyword can also delete the dictionary completely: thisdict = {   "brand": "Ford",   "model": "Mustang",   "year": 1964 }del thisdictprint(thisdict) #this will cause an error because "thisdict" no longer exists.

Copy a Dictionary

Copy a DictionaryYou cannot copy a dictionary simply by typing dict2 = dict1, because: dict2 will only be a reference to dict1, and changes made in dict1 will automatically also be made in dict2.There are ways to make a copy, one way is to use the built-in Dictionary method copy().ExampleMake a copy of a dictionary with the copy() method: thisdict = {   "brand": "Ford",   "model": "Mustang",   "year": 1964 }mydict = thisdict.copy() print(mydict) Another way to make a copy is to use the built-in method dict().ExampleMake a copy of a dictionary with the dict() method: thisdict = {   "brand": "Ford",   "model": "Mustang",   "year": 1964 }mydict = dict(thisdict) print(mydict)

Nested Dictionaries

Nested DictionariesA dictionary can also contain many dictionaries, this is called nested dictionaries.ExampleCreate a dictionary that contain three dictionaries: myfamily = { "child1" : {   "name" : "Emil",    "year" : 2004 }, "child2" : {    "name" : "Tobias",   "year" : 2007 },  "child3" : {   "name" : "Linus",    "year" : 2011 }} Or, if you want to nest three dictionaries that already exists as dictionaries:ExampleCreate three dictionaries, than create one dictionary that will contain the other three dictionaries: child1 = { "name" : "Emil", "year" : 2004}child2 = {  "name" : "Tobias", "year" : 2007}child3 = { "name" : "Linus",  "year" : 2011}myfamily = { "child1" : child1,  "child2" : child2, "child3" : child3}

The dict() Constructor

The dict() ConstructorIt is also possible to use the dict() constructor to make a new dictionary:Example thisdict = dict(brand="Ford", model="Mustang", year=1964) # note that keywords are not string literals # note the use of equals rather than colon for the assignment print(thisdict)

Python If ... Else

Python If ... ElsePython Conditions and If statementsPython supports the usual logical conditions from mathematics:Equals: a == bNot Equals: a != bLess than: a < bLess than or equal to: a <= bGreater than: a > bGreater than or equal to: a >= bThese conditions can be used in several ways, most commonly in "if statements" and loops.An "if statement" is written by using the if keyword.ExampleIf statement: a = 33 b = 200 if b > a: print("b is greater than a") In this example we use two variables, a and b, which are used as part of the if statement to test whether b is greater than a. As a is 33, and b is 200, we know that 200 is greater than 33, and so we print to screen that "b is greater than a".IndentationPython relies on indentation (whitespace at the beginning of a line) to define scope in the code. Other programming languages often use curly-brackets for this purpose.ExampleIf statement, without indentation (will raise an error): a = 33 b = 200 if b > a: print("b is greater than a") # you will get an error

Elif ...Else

ElifThe elif keyword is pythons way of saying "if the previous conditions were not true, then try this condition".Example a = 33 b = 33 if b > a:  print("b is greater than a") elif a == b:  print("a and b are equal") In this example a is equal to b, so the first condition is not true, but the elif condition is true, so we print to screen that "a and b are equal".ElseThe else keyword catches anything which isn't caught by the preceding conditions.Example a = 200 b = 33 if b > a:  print("b is greater than a") elif a == b:  print("a and b are equal") else:  print("a is greater than b")

Short Hand If ... Else

Short Hand IfIf you have only one statement to execute, you can put it on the same line as the if statement.ExampleOne line if statement: if a > b: print("a is greater than b") Short Hand If ... ElseIf you have only one statement to execute, one for if, and one for else, you can put it all on the same line:ExampleOne line if else statement: a = 2b = 330print("A") if a > b else print("B") You can also have multiple else statements on the same line:ExampleOne line if else statement, with 3 conditions: a = 330b = 330print("A") if a > b else print("=") if a == b else print("B")

And....Or

AndThe and keyword is a logical operator, and is used to combine conditional statements:ExampleTest if a is greater than b, AND if c is greater than a: a = 200b = 33c = 500if a > b and c > a:  print("Both conditions are True") OrThe or keyword is a logical operator, and is used to combine conditional statements:ExampleTest if a is greater than b, OR if a is greater than c: a = 200b = 33c = 500if a > b or a > c:  print("At least one of the conditions is True")

Nested If

Nested IfYou can have if statements inside if statements, this is called nested if statements.Example x = 41if x > 10:  print("Above ten,") if x > 20:   print("and also above 20!") else:   print("but not above 20.") The pass Statementif statements cannot be empty, but if you for some reason have an if statement with no content, put in the pass statement to avoid getting an error.Example a = 33b = 200if b > a: pass

Winter Of Code - Coding Competition

Are you thinking of getting Internship and Placement at the biggest Tech-giants Microsoft, Amazon, Samsung?We are here for you with the biggest opportunity to practice and experience a test of the same level !!Winter of CodesIt’s a free online Coding Competition Eligibility: Working Professionals and Students from all years can participate.Date: Every Saturday, December.Time : 9:00p.mDuration : 90minsWhat's there in the test?Coding, Aptitude and Logical ReasoningRewards :-Cash Prizes-MI Bands-Free Online Courses And much more...Competing with more than 150 colleges from all over India will give you the grand opportunity to face competition!Willing to participate?_Register now and visit the link for more details :https://codingninjas.in/events/winter-of-codes?campaign=winterofcodes&source=ca405

Solved programs for Python

It will give a basic idea of python programming and relate with other programming languages.

Python Data Types

Python Data TypesData types are the classification or categorization of data items. Data types represent a kind of value which determines what operations can be performed on that data. Numeric, non-numeric and Boolean (true/false) data are the most used data types. However, each programming language has its own classification largely reflecting its programming philosophy.Python has the following standard or built-in data types:NumericA numeric value is any representation of data which has a numeric value. Python identifies three types of numbers:Integer: Positive or negative whole numbers (without a fractional part)Float: Any real number with a floating point representation in which a fractional component is denoted by a decimal symbol or scientific notationComplex number: A number with a real and imaginary component represented as x+yj. x and y are floats and j is -1(square root of -1 called an imaginary number)BooleanData with one of two built-in values True or False. Notice that 'T' and 'F' are capital. true and false are not valid booleans and Python will throw an error for them.Sequence TypeA sequence is an ordered collection of similar or different data types. Python has the following built-in sequence data types:String: A string value is a collection of one or more characters put in single, double or triple quotes.List : A list object is an ordered collection of one or more data items, not necessarily of the same type, put in square brackets.Tuple: A Tuple object is an ordered collection of one or more data items, not necessarily of the same type, put in parentheses.DictionaryA dictionary object is an unordered collection of data in a key:value pair form. A collection of such pairs is enclosed in curly brackets. For example: {1:"Steve", 2:"Bill", 3:"Ram", 4: "Farha"}type() functionPython has an in-built function type() to ascertain the data type of a certain value. For example, enter type(1234) in Python shell and it will return <class 'int'>, which means 1234 is an integer value. Try and verify the data type of different values in Python shell, as shown below.>>> type(1234)<class 'int'>>>> type(55.50)<class 'float'>>>> type(6+4j)<class 'complex'>>>> type("hello")<class 'str'>>>> type([1,2,3,4])<class 'list'>>>> type((1,2,3,4))<class 'tuple'>>>> type({1:"one", 2:"two", 3:"three"})<class 'dict'>Mutable and Immutable ObjectsData objects of the above types are stored in a computer's memory for processing. Some of these values can be modified during processing, but contents of others can't be altered once they are created in the memory.Number values, strings, and tuple are immutable, which means their contents can't be altered after creation.On the other hand, collection of items in a List or Dictionary object can be modified. It is possible to add, delete, insert, and rearrange items in a list or dictionary. Hence, they are mutable objects.