2. • What is Python?
• Features of Python
• Who uses Python?
• Applications
• How to download python
• Working with interactive prompt
• Dynamic
• Identifiers
• Keywords
• Python Syntax
CONTENTS (Day1)
3. • Python is a general purpose, dynamic, high-level, and
interpreted programming language.
• Created by Guido Van Rossum in 1989.
What is Python
4. 1) Easy to Learn and Use
2) Expressive Language
3) Interpreted Language
4) Cross-platform Language
5) Free and Open Source-https://www.python.org/
6) Object-Oriented Language
7) Extensible
8) Large Standard Library
9) GUI Programming Support
10) Integrated
Python Features
14. • When assigning to a variable, we do not need to
declare the data type of the values it will hold. This is
decided by the interpreter at runtime.
• We do not declare a variable, we directly assign to it.
Python is Dynamically-Typed
15. • A variable name is called an identifier and has to
follow some rules:
• Variables can have letters (A-Z and a-z), digits(0-9)
and underscores.
• It cannot begin with an underscore (_) or a digit.
• It cannot have whitespace and signs like + and -,
!, @, $, #, %.
• It cannot be a reserved keyword for Python.
• Variable names are case sensitive.
Identifiers
16. • These are 33 reserved words.
• You cannot use a word from this list as a name for
your variable or function.
• Python variable names are case-sensitive. The
variable ‘name’ is different than the variable ‘Name’
Reserved Keywords
17. • The term syntax is referred to a set of rules and
principles that describes the structure of a language.
Python Syntax
18. A Python program comprises logical lines. A NEWLINE token
follows each of those. The interpreter ignores blank lines.
The following line causes an error.
>>> print("Hi
How are you?")
Output:
SyntaxError: EOL while scanning string literal
Python Line Structure
19. • This one is an important Python syntax.
• We saw that Python does not mandate semicolons.
• A new line means a new statement. But sometimes, you may
want to split a statement over two or more lines.
Python Multiline Statements
20. • A docstring is a documentation string.
• As a comment, this Python Syntax is used to explain code.
• But unlike comments, they are more specific. Also, they are
retained at runtime. This way, the programmer can inspect
them at runtime.
"""
This function prints out a greeting
"""
print("Hi")
Python Docstrings
21. • Python doesn’t use curly braces to delimit blocks of code, this
Python Syntax is mandatory.
• You can indent code under a function, loop, or class.
• >>> if 2>1:
print("2 is the bigger person");
print("But 1 is worthy too");
Python Indentation
22. You can also fit in more than one statement on one line.
Do this by separating them with a semicolon.
>>> a=7;print(a);
Python Multiple Statements in One
Line
23. Python supports the single quote and the double quote for string
literals.
But if you begin a string with a single quote, you must end it with
a single quote. The same goes for double-quotes.
• >>> print('We need a break');
• >>> print("We need a ‘break'");
Python Quotations
24. • You can use the % operator to format a string to contain text
as well as values of identifiers.
• Use %s where you want a value to appear. After the string,
put a % operator and mention the identifiers in parameters.
Python String Formatters
I just printed 10 pages to the printer HP
26. • You can use the % operator to format a string to contain text
as well as values of identifiers.
• Use %s where you want a value to appear. After the string,
put a % operator and mention the identifiers in parameters.
Python String Formatters
I just printed 10 pages to the printer HP
27. Python Syntax ‘Comments’ let you store tags at the
right places in the code. You can use them to explain
complex sections of code. The interpreter ignores
comments. Declare a comment using an Hash(#).
>>>#this is a comment line
Python Comments
28. • Python allows us to assign a value to multiple
variables in a single statement which is also known as
multiple assignment.
• We can apply multiple assignments in two ways
either by assigning a single value to multiple
variables
or
• assigning multiple values to multiple variables.
Multiple Assignment
35. Python Data Types
• Variables can hold values of different data types.
• Python is a dynamically typed language hence we
need not define the type of the variable while
declaring it.
• The interpreter implicitly binds the value with its
type.
36. Python Data Types
• python enables us to check the type of the variable used in the program.
• Python provides us the type() function which returns the type of the
variable passed.
37. Python Data Types
Python data types are categorized into two as follows:
Mutable Data Types: Data types in python where the value
assigned to a variable can be changed
Immutable Data Types: Data types in python where the
value assigned to a variable cannot be changed
39. Python-Numbers
• Number data types store numeric values.
• They are immutable data types, means that changing
the value of a number data type is not possible.
• For example −
• var1 = 1 var2 = 10
• You can also delete the reference to a number object
by using the del statement.
• Del var1
40. Python-Numbers
• Python supports four different numerical types −
• int (signed integers) − They are often called just integers or ints,
are positive or negative whole numbers with no decimal point.
• Long (long integers ) − Also called longs, they are integers of
unlimited size, written like integers and followed by an uppercase
or lowercase L.
• float (floating point real values) − Also called floats, they
represent real numbers and are written with a decimal point
dividing the integer and fractional parts.
• complex (complex numbers) − are of the form a + bJ, where a and
b are floats and J (or j) represents the square root of -1 (which is an
imaginary number).
41. Number Type Conversion
• But sometimes, you need to convert a number explicitly from
one type to another to satisfy the requirements of an
operator or function parameter.
• Type int(x) to convert x to a plain integer.
• Type long(x) to convert x to a long integer.
• Type float(x) to convert x to a floating-point number.
• Type complex(x) to convert x to a complex number with real
part x and imaginary part zero.
• Type complex(x, y) to convert x and y to a complex number
with real part x and imaginary part y. x and y are numeric
expressions
43. Strings
• Strings are amongst the most popular types in
Python.
• We can create them simply by enclosing characters in
quotes. Python treats single quotes the same as
double quotes.
• var1 = 'Hello World!‘
• var2 = "Python Programming"
44. Accessing Values in Strings
• Python does not support a character type; these are treated
as strings of length one, thus also considered a substring.
• To access substrings, use the square brackets for slicing along
with the index or indices to obtain your substring.
45. Updating Strings
• You can "update" an existing string by (re)assigning a variable
to another string. The new value can be related to its previous
value or to a completely different string altogether.
48. Python Lists
• Lists are the most versatile of Python's compound
data types.
• A list contains items separated by commas and
enclosed within square brackets ([]).
• To some extent, lists are similar to arrays in C.
• One difference between them is that all the items
belonging to a list can be of different data type.
50. Python Tuples
• A tuple is another sequence data type that is similar to
the list.
• A tuple consists of a number of values separated by
commas.
• Unlike lists, however, tuples are enclosed within
parentheses.
• The main differences between lists and tuples are: Lists
are enclosed in brackets ( [ ] ) and their elements and size
can be changed,
• while tuples are enclosed in parentheses ( ( ) ) and
cannot be updated.
• Tuples can be thought of as read-only lists.
52. Python Dictionary
• A Python dictionary is a mapping of unique keys to
values. Dictionaries are mutable, which means they
can be changed.
• Dictionaries are enclosed by curly braces ({ }) and
values can be assigned and accessed using square
braces ([]).
54. Dictionary Functions
• Len-The method len() gives the total length of the
dictionary.
• dict = {'Name': 'Zara', 'Age': 7};
• print (len(dict))
• Copy-The method copy() returns a shallow copy of the
dictionary.
• dict1 = {'Name': 'Zara', 'Age': 7};
• dict2 = dict1.copy()
• print "New Dictionary : %s" % str(dict2)
56. • Keys-The method keys() returns a list of all the
available keys in the dictionary.
• my_dict = {'name':'Jack', 'age': 26}
• print "Value : %s" % dict.keys()
• Values-The method values() returns a list of all
the values available in a given dictionary.
• print "Value : %s" % dict.values()
58. Set in Python
• A set is an unordered collection of items.
• Every element is unique (no duplicates) and
must be immutable (which cannot be
changed).
• my_set = {1, 2, 3}
• print(my_set)
59. Change a set in Python
• Sets are mutable. But since they are unordered,
indexing have no meaning.
• We can add single element using
the add() method and multiple elements using
the update() method.
• The update() method can take tuples,
lists, strings or other sets as its argument. In all
cases, duplicates are avoided.
61. Remove elements from a set
• A particular item can be removed from set using
methods, discard() and remove().
• The only difference between the two is that,
while using discard() if the item does not exist in
the set, it remains unchanged.
• But remove() will raise an error in such condition.
• my_set.discard(4)
• print(my_set)
• my_set.remove(4)