Knowledge in python tutorial

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

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.

SUM OF N NUMBERS OF A LIST IN PYTHON

This pdf consists of sample PROGRAM FOR ADDING N NUMBERS INSIDE A LIST MADE IN PYTHON which can be useful to the Second Year Students who are currently pursuing CSE. Also, with the help of the above assignment, I hope your doubts get solved and will help you achieve good grades in end semesters or your mid semesters. Solved by one of the students at VJTI, I hope it will help you achieve good grades in exam.

COMPLETE PYTHON PROGRAMMING

Python is an interpreted, high-level, general-purpose programming language. Created by Guido van Rossum and first released in 1991, Python's design philosophy emphasizes code readability with its notable use of significant whitespace. Its language constructs and object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects.

Basic Python Introduction

This document is containing Basic Python Introduction

Python Syntax

This document is containing brief discussion about Python Syntax.

Python Variable

This document is containing brief discussion about Python Variables.

Python Data Types

This document is containing brief discussion about Python Data Types.