Sie sind auf Seite 1von 16

PYTHON

A Course Report
(Course Report for the partial fulfillment in awarding the degree of B.Tech in Computer

Science & Engineering.)

Submitted by

SAURAB RAWAT

(Roll No. 180050101047)

Under the Supervision & Guidance of

Mr. NITIN CHHIMWAL

Submitted to:

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BIRLA INSTITUTE OF APPLIED SCIENCES, BHIMTAL

NAINITAL (UTTARAKHAND) - 263136

August 2019
TABLE OF CONTENTS

Sr. No. CONTENT Page No


1 Abstract 01
2 Acknowledgement 02
3 Introduction about python 03
4 Basic Data Type In Python 04
5 Strings 05
6 Built-in Functions 06
7-8 Python List & Python Tuples 07
9 Python dictionary 08
10 Booleans 09
11 Sets in Python 10
12 Methods for Sets 10
13 Python Statement and Indentation 11
14 Conclusion 12
15 Reference 13
16 Certificate 14
ABSTRACT

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 is dynamically typed and garbage-collected. It supports multiple programming paradigms,


including procedural, object-oriented, and functional programming. Rather than having all of its
functionality built into its core, Python was designed to be highly extensible.

This compact modularity has made it particularly popular as a means of adding programmable interfaces
to existing applications. Van Rossum's vision of a small core language with a large standard library and
easily extensible interpreter stemmed from his frustrations with ABC, which espoused the opposite
approach.
ACKNOWLEDGEMENT

I like to express my deep sense of gratitude to my Mentor Dr. Sandesh Tripathi , coordinator of
Department of Computer Science & Engineering, Birla Institute of Applied Sciences, Bhimtal,
Uttarakhand for his guidance, keen interest, constant encouragement and helpful criticism.

His advices were invaluable even when the going got tough during the course of the study embodied in
this project.

My deepest gratitude also goes for our respected Director Sir Dr. B.S. Bisht and

Co-Ordinator of the Department, Dr. Sandesh Tripathi for his valuable advice,

encouragement and all time support.

I would like to thank all faculties and staff-members of the Department of Computer

Science & Engineering, BIAS for their help and suggestions for betterment of the project.

I would also like to acknowledge the Dept. of CSE for providing Support, Enthusiasm

and other such lab facilities and the University of Dehradun , for providing infrastructure

development fund.

I would like to thank my friends and colleagues for critically reading the manuscript and

providing information at times.

I am also highly grateful to my parents for their endless support.

Name: Saurab Rawat

Roll No : 180050101047 Date: 16/08/2019


ABOUT PYTHON

The Language core philosophy is summarized in the document, The Zen of Python (PEP 20) which
includes aphorisms such as....

• Beautiful is better than ugly


• Simple is better than complex
• Complex is better than complicated

The software development companies prefer Python language because of its versatile features and fewer
programming codes. Nearly 14% of the programmers use it on the operating systems like UNIX, Linux,
Windows and Mac OS. The programmers of big companies use Python as it has created a mark for itself
in the software development with characteristic features like-Interactive, Interpreted, Modular, Dynamic,
Portable, High level, Extensible in C++ & C.

Some of its advantages are:

1. Extensive library support: It provides large standard libraries that include the areas like string
operations, Internet, web service tools, operating system interfaces and protocols.

2. Integration Feature: Python integrates the Enterprise Application Integration that makes it easy to
develop Web services by invoking COM or COBRA components. It has powerful control capabilities as
it calls directly through C, C++ or Java via Python.

3. Improved Programmer’s Productivity: The language has extensive support libraries and clean
object-oriented designs that increase two to tenfold of programmer’s productivity while using the
languages like Java, VB, Perl, C, C++ and C#.

4. Productivity: With its strong process integration features, unit testing framework and enhanced
control capabilities contribute towards the increased speed for most applications and productivity of
applications.
BASIC DATA TYPES IN PYTHON
An introduction to the basic concepts of Python. Learn how to use Python interactively and through a
script. Create your first variables and acquaint yourself with Python's basic data types.

DATA TYPES

Data Type Data types determine whether an object can do something, or whether it just would not make
sense. Other programming languages often determine whether an operation makes sense for an object by
making sure the object can never be stored somewhere where the operation will be performed on the
object (this type system is called static typing). Python does not do that. Instead it stores the type of an
object with the object, and checks when the operation is performed whether that operation makes sense
for that object (this is called dynamic typing). Python has many native data types. Here are the important
ones:

1. Boolean are either True or False.

2. Numbers can be integers (1 and 2), floats (1.1 and 1.2), fractions (1/2 and 2/3), or even complex
numbers.

3. Strings are sequences of Unicode characters, e.g. an HTML document.

4. Bytes and byte arrays, e.g. a JPEG image file.

5. Lists are ordered sequences of values.

6. Tuples are ordered, immutable sequences of values.

7. Sets are unordered bags of values.

VARIABLES:

Variables are containers for storing data values.


Unlike other programming languages, Python has no command for declaring a variable.
A variable is created the moment you first assign a value to it.

Example

x = 5
y = "John"
print(x)
print(y)

A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume). Rules for
Python variables:

• A variable name must start with a letter or the underscore character


• A variable name cannot start with a number
• A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
• Variable names are case-sensitive (age, Age and AGE are three different variables)
STRINGS:
• Strings are sequences of character data. The string type in Python is called str.

• String literals may be delimited using either single or double quotes. All the characters between the
opening delimiter and matching closing delimiter are part of the string:

To create a string, put the sequence of characters inside either single quotes, double quotes, or triple quotes and
then assign it to a variable. You can look into how variables work in Python in the Python variables tutorial.

For example, you can assign a character ‘a’ to a variable single_quote_character. Note that the string is a single
character and it is “enclosed” by single quotes.

>>> single_quote_character = 'a'


>>> print(single_quote_character)
a
>>> print(type(single_quote_character)) # check the type of the variable.
<class 'str'>

Applying Special Meaning to Characters

Next, suppose you need to create a string that contains a tab character in it. Some text editors may allow you to
insert a tab character directly into your code. But many programmers consider that poor practice, for several
reasons:

• The computer can distinguish between a tab character and a sequence of space characters, but you can’t. To
a human reading the code, tab and space characters are visually indistinguishable.

• Some text editors are configured to automatically eliminate tab characters by expanding them to the
appropriate number of spaces.

• Some Python REPL environments will not insert tabs into code.

In Python (and almost all other common computer languages), a tab character can be specified by the escape
sequence \t:

PYTHON
>>> print('foo\tbar')
foo bar

The escape sequence \t causes the t character to lose its usual meaning, that of a literal t. Instead, the combination is
interpreted as a tab character.
Built-In Functions:-
The Python interpreter supports many functions that are built-in: sixty-eight, as of Python 3.6. You will cover many
of these in the following discussions, as they come up in context.

For now, a brief overview follows, just to give a feel for what is available. See the Python documentation on built-
in functions for more detail. Many of the following descriptions refer to topics and concepts that will be discussed
in future tutorials.

Math
Function Description

abs() Returns absolute value of a number

divmod() Returns quotient and remainder of integer division

max() Returns the largest of the given arguments or items in an iterable

min() Returns the smallest of the given arguments or items in an iterable

pow() Raises a number to a power

round() Rounds a floating-point value

sum() Sums the items of an iterable

Type Conversion

Function Description

ascii() Returns a string containing a printable representation of an object

str() Returns a string version of an object

bool() Converts an argument to a Boolean value

chr() Returns string representation of character given by integer argument

complex() Returns a complex number constructed from arguments

float() Returns a floating-point object constructed from a number or string

hex() Converts an integer to a hexadecimal string

int() Returns an integer object constructed from a number or string

oct() Converts an integer to an octal string

type() Returns the type of an object or creates a new type object

repr() Returns a string containing a printable representation of an object


Python List

List is an ordered sequence of items. It is one of the most used datatype in Python and is very flexible. All the items
in a list do not need to be of the same type.

Declaring a list is pretty straight forward. Items separated by commas are enclosed within brackets [ ].

1. >>> a = [1, 2.2, 'python']

We can use the slicing operator [ ] to extract an item or a range of items from a list. Index starts form 0 in Python.

a = [5,10,15,20,25,30,35,40]

# a[2] = 15

print("a[2] = ", a[2])

# a[0:3] = [5, 10, 15]

print("a[0:3] = ", a[0:3])

# a[5:] = [30, 35, 40]

print("a[5:] = ", a[5:])

Lists are mutable, meaning, value of elements of a list can be altered.

1. >>> a = [1,2,3]
2. >>> a[2]=4
3. >>> a
4. [1, 2, 4]

Python Tuple

Tuple is an ordered sequence of items same as list.The only difference is that tuples are immutable. Tuples once
created cannot be modified.

Tuples are used to write-protect data and are usually faster than list as it cannot change dynamically.

It is defined within parentheses () where items are separated by commas.

1. >>> t = (5,'program', 1+3j)

We can use the slicing operator [] to extract items but we cannot change its value.
Python Dictionary

Dictionary is an unordered collection of key-value pairs.

It is generally used when we have a huge amount of data. Dictionaries are optimized for retrieving data. We must
know the key to retrieve the value.

In Python, dictionaries are defined within braces {} with each item being a pair in the form key:value. Key and
value can be of any type.

1. >>> d = {1:'value','key':2}
2. >>> type(d)
3. <class 'dict'>

Conversion between data types


We can convert between different data types by using different type conversion functions like int(), float(), str()
etc.

1. >>> float(5)
2. 5.0

* Conversion from float to int will truncate the value (make it closer to zero).

1. >>> int(10.6)
2. 10
3. >>> int(-10.6)
4. -10

• Conversion to and from string must contain compatible values.

1. >>> float('2.5')
2. 2.5
3. >>> str(25)
4. '25'
5. >>> int('1p')
6. Traceback (most recent call last):
7. File "<string>", line 301, in runcode
8. File "<interactive input>", line 1, in <module>
9. ValueError: invalid literal for int() with base 10: '1p'

• We can even convert one sequence to another.

1. >>> set([1,2,3])
2. {1, 2, 3}
3. >>> tuple({5,6,7})
4. (5, 6, 7)
5. >>> list('hello')
6. ['h', 'e', 'l', 'l', 'o']

• To convert to dictionary, each element must be a pair

1. >>> dict([[1,2],[3,4]])
2. {1: 2, 3: 4}
3. >>> dict([(3,26),(4,44)])
4. {3: 26, 4: 44}
Booleans, True or False in Python
Boolean values are the two constant objects False and True.
They are used to represent truth values (other values can also be considered false or true).
In numeric contexts (for example, when used as the argument to an arithmetic operator), they behave like the
integers 0 and 1, respectively.
The built-in function bool() can be used to cast any value to a Boolean, if the value can be interpreted as a truth
value They are written as False and True, respectively.

Boolean Strings
A string in Python can be tested for truth value.

The return type will be in Boolean value (True or False)

Let’s make an example, by first create a new variable and give it a value.

my_string = "Hello World"


my_string.isalnum() #check if all char are numbers
my_string.isalpha() #check if all char in the string are alphabetic
my_string.isdigit() #test if string contains digits
my_string.istitle() #test if string contains title words

To see what the return value (True or False) will be, simply print it out.

print my_string.isalnum() #False


print my_string.isalpha() #False
print my_string.isdigit() #False
print my_string.istitle() #True

Boolean and logical operators


Boolean values respond to logical operators and / or
>>> True and False
False
>>> True and True
True>>> False and True
False
>>> False or True
True
>>> False or False
False
Sets in Python
A Set is an unordered collection data type that is iterable, mutable, and has no duplicate elements. Python’s set
class represents the mathematical notion of a set. The major advantage of using a set, as opposed to a list, is that it
has a highly optimized method for checking whether a specific element is contained in the set. This is based on a
data structure known as a hash table.

Frozen Sets Frozen sets are immutable objects that only support methods and operators that produce a result
without affecting the frozen set or sets to which they are applied.

# Python program to demonstrate differences


# between normal and frozen set

I/P
normal_set = set(["a", "b","c"])
normal_set.add("d")
print("Normal Set")
print(normal_set)
frozen_set = frozenset(["e", "f", "g"])
print("Frozen Set")
print(frozen_set)

O/P
Normal Set
set(['a', 'c', 'b', 'd'])
Frozen Set
frozenset(['e', 'g', 'f'])

Methods for Sets


1. add(x) Method: Adds the item x to set if it is not already present in the set.

people = {"Jay", "Idrish", "Archil"}


people.add("Daxit")

2. union(s) Method: Returns a union of two set.Using the ‘|’ operator between 2 sets is the same as writing
set1.union(set2)

people = {"Jay", "Idrish", "Archil"}


vampires = {"Karan", "Arjun"}
population = people.union(vampires)

3. intersect(s) Method: Returns an intersection of two sets. The ‘&’ operator comes can also be used in this case.

victims = people.intersection(vampires)
Python Statement and Indentation

Instructions that a Python interpreter can execute are called statements. For example, a = 1 is an assignment
statement. if statement, for statement, while statement etc. are other kinds of statements.

Python Indentation

Most of the programming languages like C, C++, Java use braces { } to define a block of code. Python uses
indentation.

A code block (body of a function, loop etc.) starts with indentation and ends with the first unindented line. The
amount of indentation is up to you, but it must be consistent throughout that block.

Generally four whitespaces are used for indentation and is preferred over tabs. Here is an example.

1. for i in range(1,11):
2. print(i)
3. if i == 5:
4. Break

The enforcement of indentation in Python makes the code look neat and clean. This results into Python programs
that look similar and consistent.

Indentation can be ignored in line continuation. But it's a good idea to always indent. It makes the code more
readable. For example:

1. if True:
2. print('Hello')
3. a = 5

and

1. if True: print('Hello'); a = 5

both are valid and do the same thing. But the former style is clearer.

Incorrect indentation will result into Indentation Error.

Python Comments
Comments are very important while writing a program. It describes what's going on inside a program so that a
person looking at the source code does not have a hard time figuring it out. You might forget the key details of the
program you just wrote in a month's time. So taking time to explain these concepts in form of comments is always
fruitful.
It extends up to the newline character. Comments are for programmers for better understanding of a program.
Python Interpreter ignores comment.

1. #This is a comment
2. #print out Hello
3. print('Hello')
CONCLUSION

The course was Python 3 for Begginers. The video lectures of the course contained a native approach to the
topics they covered. They explained the theoretical portion of the course using living/daily examples
whereas the portion related to programming concepts was covered using Pycharm or Jupyter notebook
itself and hence showing the procedural way of writing and executing the code snippet.

The course covers all the concept of Python, from beginner to intermediate level. The concept regarding
the algorithms, list, searching, sorting, array vs lists was very precisely covered.
As we kept on moving the course becomes slightly difficult and confusing because of the introduction to
complex analysis and asymptotic notation using data structure.

The course was introduced for the basis of programming concepts in Python Language. The course
contains videos lectures of python in which almost all of the key concept of Basic Python Programming is
explained.

The instructor has put all his efforts to make himself clear. The topics covered by him are “About Python”.
It’s advantages, variables, python list, bytes, tuple, sequential datatype, code introspection etc.

After coding the programs with static memory allocation we move onto the concept of dynamic memory
allocation using linked list. The concept of linked list is a little confusing to understand. And then we
finally move on to the concept of data file handling which gives you a basic approach to statements in
python. The course was covered with a highly appreciable approach.
REFRENCES

➢ https://www.w3schools.com
➢ https://github.com/karan/Projects
➢ https://wiki.python.org/moin/CodingProjectIdeas
➢ https://en.wikipedia.org
➢ https://www.tutorialspoint.com
➢ https://www.google.com

Das könnte Ihnen auch gefallen