Knowledge in python basics

Python Booleans

Python BooleansBooleans represent one of two values: True or False.Boolean ValuesIn programming you often need to know if an expression is True or False.You can evaluate any expression in Python, and get one of two answers, True or False.When you compare two values, the expression is evaluated and Python returns the Boolean answer:Example print(10 > 9)print(10 == 9)print(10 < 9) When you run a condition in an if statement, Python returns True or False:ExamplePrint a message based on whether the condition is True or False: a = 200b = 33if b > a: print("b is greater than a") else: print("b is not greater than a") Evaluate Values and VariablesThe bool() function allows you to evaluate any value, and give you True or False in return,ExampleEvaluate a string and a number: print(bool("Hello"))print(bool(15)) ExampleEvaluate two variables: x = "Hello"y = 15print(bool(x))print(bool(y))

Solved programs for Python

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

Python Function

Python FunctionsIn this article, you'll learn about functions, what a function is, the syntax, components, and types of functions. Also, you'll learn to create a function in Python.What is a function in Python?In Python, a function is a group of related statements that performs a specific task.Functions help break our program into smaller and modular chunks. As our program grows larger and larger, functions make it more organized and manageable.Furthermore, it avoids repetition and makes the code reusable.Syntax of Functiondef function_name(parameters): """docstring""" statement(s) Above shown is a function definition that consists of the following components.Keyword def that marks the start of the function header.A function name to uniquely identify the function. Function naming follows the same rules of writing identifiers in Python.Parameters (arguments) through which we pass values to a function. They are optional.A colon (:) to mark the end of the function header.Optional documentation string (docstring) to describe what the function does.One or more valid python statements that make up the function body. Statements must have the same indentation level (usually 4 spaces).An optional return statement to return a value from the function.Example of a functiondef greet(name): """ This function greets to the person passed in as a parameter """ print("Hello, " + name + ". Good morning!") How to call a function in python?Once we have defined a function, we can call it from another function, program or even the Python prompt. To call a function we simply type the function name with appropriate parameters.>>> greet('Paul') Hello, Paul. Good morning! Note: Try running the above code in the Python program with the function definition to see the output.def greet(name): """ This function greets to the person passed in as a parameter """ print("Hello, " + name + ". Good morning!") greet('Paul') Output:Hello, Paul. Good morning!DocstringsThe first string after the function header is called the docstring and is short for documentation string. It is briefly used to explain what a function does.Although optional, documentation is a good programming practice. Unless you can remember what you had for dinner last week, always document your code.In the above example, we have a docstring immediately below the function header. We generally use triple quotes so that docstring can extend up to multiple lines. This string is available to us as the __doc__ attribute of the function.For example:Try running the following into the Python shell to see the output.>>> print(greet.__doc__) This function greets to the person passed in as a parameter  To learn more about docstrings in Python, visit Python Docstrings.The return statementThe return statement is used to exit a function and go back to the place from where it was called.Syntax of returnreturn [expression_list] This statement can contain an expression that gets evaluated and the value is returned. If there is no expression in the statement or the return statement itself is not present inside a function, then the function will return the None object.For example:>>> print(greet("May")) Hello, May. Good morning! None

Python Classes and Objects

Objects are an encapsulation of variables and functions into a single entity. Objects get their variables and functions from classes. Classes are essentially a template to create your objects.A very basic class would look something like this:script.py12345class MyClass: variable = "blah" def function(self): print("This is a message inside the class.")IPython ShellIn [1]: RunPowered by DataCampWe'll explain why you have to include that "self" as a parameter a little bit later. First, to assign the above class(template) to an object you would do the following:script.py1234567class MyClass: variable = "blah" def function(self): print("This is a message inside the class.")myobjectx = MyClass()IPython ShellIn [1]: RunPowered by DataCampNow the variable "myobjectx" holds an object of the class "MyClass" that contains the variable and the function defined within the class called "MyClass".Accessing Object VariablesTo access the variable inside of the newly created object "myobjectx" you would do the following:script.py123456789class MyClass: variable = "blah" def function(self): print("This is a message inside the class.")myobjectx = MyClass()myobjectx.variableIPython ShellIn [1]: RunPowered by DataCampSo for instance the below would output the string "blah":script.py123456789class MyClass: variable = "blah" def function(self): print("This is a message inside the class.")myobjectx = MyClass()print(myobjectx.variable)IPython ShellIn [1]: RunPowered by DataCampYou can create multiple different objects that are of the same class(have the same variables and functions defined). However, each object contains independent copies of the variables defined in the class. For instance, if we were to define another object with the "MyClass" class and then change the string in the variable above:script.py1234567891011121314class MyClass: variable = "blah" def function(self): print("This is a message inside the class.")myobjectx = MyClass()myobjecty = MyClass()myobjecty.variable = "yackity"# Then print out both valuesprint(myobjectx.variable)print(myobjecty.variable)IPython ShellIn [1]: RunPowered by DataCampAccessing Object FunctionsTo access a function inside of an object you use notation similar to accessing a variable:script.py123456789class MyClass: variable = "blah" def function(self): print("This is a message inside the class.")myobjectx = MyClass()myobjectx.function()IPython ShellIn [1]: RunPowered by DataCampThe above would print out the message, "This is a message inside the class."ExerciseWe have a class defined for vehicles. Create two new vehicles called car1 and car2. Set car1 to be a red convertible worth $60,000.00 with a name of Fer, and car2 to be a blue van named Jump worth $10,000.00.script.py1234567891011121314# define the Vehicle classclass Vehicle: name = "" kind = "car" color = "" value = 100.00 def description(self): desc_str = "%s is a %s %s worth $%.2f." % (self.name, self.color, self.kind, self.value) return desc_str# your code goes here# test codeprint(car1.description())print(car2.description())IPython ShellIn [1]: SolutionRun

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.

Prime number Generator Code in Python

Here I have attached the snippets of the code and the output generated along with the code in written form. It consists of a code in python written in notepad wherein we learn to print prime numbers by giving the input of range and the number of numbers we want to print. Copy the code in any editor of your choice and save it with .py extension and run it in an online interpreter or your python command prompt.

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.

Python Programming Language

This contains the basic handwritten notes on python programming language that we have studied in 3rd semester. It contains knowledge of strings, textfiles and book of python crash course. Python Crash Course is a fast-paced, thorough introduction to Python that will have you writing programs, solving problems, and making things that work in no time.

Photo Gallary Assignment

photo gallary assignment coursera

Fundamentals of python programming

This document consist of the basics of python language. It covers details like introduction,versions of python, pros and cons and step by step procedure to download python on your PC.

Problem solving and python programming notes

This file posses cut short explanations for the important questions for university examinations. This covers topics like list,tuples,dictionaries and control flow structures. Each and every topic is explained in a short way along with an example.