0% found this document useful (0 votes)
2 views

Unit 1(Python)

The document provides an introduction to Python programming, highlighting its features such as simplicity, platform independence, and support for object-oriented programming. It covers Python's syntax, including identifiers, keywords, variables, data types, and comments, as well as the use of lists, tuples, and dictionaries. The document emphasizes Python's ease of learning and its extensive standard library, making it a versatile language for developers.

Uploaded by

waghkaveri5
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Unit 1(Python)

The document provides an introduction to Python programming, highlighting its features such as simplicity, platform independence, and support for object-oriented programming. It covers Python's syntax, including identifiers, keywords, variables, data types, and comments, as well as the use of lists, tuples, and dictionaries. The document emphasizes Python's ease of learning and its extensive standard library, making it a versatile language for developers.

Uploaded by

waghkaveri5
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Programming

with
‘python’
 Python has a simple syntax similar to the English language.
 Python works on different platforms(Windows, Mac, Linux, Raspberry
Pi, etc.)
 Python has syntax that allows developers to write programs with fewer
lines than some other programming languages.
 Python has too much very easy syntax as compared to other
Programming languages.
 Python relies on indentation, using whitespaces, to define scope; such
as scope of loops, functions and classes. Other programming languages
often use curly-braces ( { } ) for this purpose.
Unit I:- Introduction &
Syntax of Python
Program
 Features of Python
 Easy to learn:- Python is a high-level programming language. Every
single statement you use, it proves some similarities to the English
language.

 Interpreted:- It performs line-by-line execution of statement with


availability of in-time compilers. There is no need to compile Python
code this makes it easier to debug.

 Platform independent:- if we have python code for Windows & if we


want to run this code on other platform like Linux, Mac then we do not
need to change any single line of it.
 Features of Python
 Cross Platform & Portable:- Python can run on various operating
systems such as Mac, Windows, Linux, Unix etc.

 Object Oriented:- Python supports OOPL features like classes and


objects, Inheritance, Polymorphism, Encapsulation etc.

 Large Standard Library:- Python has a large standard library which


provides rich set of modules and functions. So you do not have to write
your own code for every single line.
 Features of Python
 Free & open-source:- Python is free to download & use. It is ex of
FLOSS (Free Open Source Software), which means you can freely read
its source code, modify it and release your personalized python
version.

 Auto-destructive:- it supports automatic memory management.

 Supports exception handling:- Means we can write less error prone


code & can test various scenarios that can cause exception later on.
 Python Building Blocks

 Python Identifiers
 Python Keywords
 Python Variables
 Python data types
 Python comments
 Python Identifiers

• Includes variable’s name, function’s name, class’s name, object’s


name(Any Name).
• It has some rules to giving identifier such as:
• Start with letter (A-Z/a-z) or underscore(_). They can have digits but
not start with.
• Their length is not limited but preferred to be meaningful.
• They should not contain white spaces.
• They should not be keywords.
 Python Keywords

• Whose meanings are already known to compiler and also


called reserved words. Python has total 35 keywords.

• False, break, del, elif, else, True, except, finally, for, from, if,
import, global, lambda, while, return………etc.
 Python variables

• Variables are the reserved memory locations to store runtime


value. Means when we create variable, it reserves a memory
location.
• Based on data type of variable, interpreter allocates memory &
decides what can be stored in variable’s memory.
• Python variable do not need explicit declaration of variables.
Declaration and creation is automatically happens when we
assign value to the variable.
• For example:
a = 3 # it will assign “integer” to “a” variable.
• This will automatically create a memory location a & assigns
3 to it.

• name = “Ram” # assigns “string” to “name”.


• avg = “4.5” # assigns “float” to “avg”.
 Python comments
• Comments are very important while writing a program. It describes
the purpose of written code.
• Python provides two types of comments.

1. Single-line comment
2. Multi-line comment
1. Single-line comment :- It comments single line of code. We have
hash(#) symbol as single-line comment .

Example: print(“Welcome to IETK”) # this is single line comment.

2. Multi-line comment :- It extends up to multiple lines.


Example: print(“Welcome to IETK”) # this can also be done
# to make a multiple
# comment section
But python provides multiline comment facility by using triple quotes,
either single( ‘ ‘ ‘ ) or double( “ “ “ ).
Example:- print(“Welcome to IETK”)
“ “ “ this can also be done
to make a multiline
comment section” ” ”

Example:- print(“Welcome to IETK”)


‘ ‘ ‘ this can also be done
to make a multiline
comment section’ ’ ’
 Python Data Types
• Python variables don’t require any specific declaration using data
type. Based on assigned value, Python will automatically assign data
type to its variables.

• Numbers
• String
• List
• Tuple
• Dictionary
1. Python Numbers
• The Number data types are to store numeric values. Python have 4
different types in Numbers.

• int
• long
• float
• complex
int
• A = 45
• B = 1765
• C = -6574
• D = 85756585959757
long
• Stores signed long integers. Internally, they can also represent
Octal or Hexadecimal values.

• A = 746484L
• B = 064D
• C = 0o75648
• D = 93788764673838333
float
• A = 45.78
• B = - 65738.876
• C = 35.2e100
• D = 36464748.9338736
complex
• It consists of an ordered pair of real floating-point numbers
denoted by x + yj, where x and y are real numbers and j is
imaginary unit.

• A = 3.14j
• 3e + 36j
• 34.33e – 36j
2. Python String
• String is a set of characters represent in single or double quotes. Python
allows 3 operators to work for a String.

I. The Slicing Operator ( [:] ):- Based on index number it retrieves substring.
Python allows backward string index where last character of String has
index -1, second last at -2 and so on.
II. The string Concatenation Operator ( + ):- It joins Strings.
III. The repetition operator ( * ):- repeat the left operand string according to
right operand numeric value.
Example:- Output:-

string = ‘Welcome to IETK’


print(string) Welcome to IETK
print(string[0]) W
print(string[3:7]) Come
print(string[-1]) K
print(string[3:]) come to IETK
print(string * 2) Welcome to IETKWelcome to IETK
print(string + ‘Kannad’) Welcome to IETK Kannad
3. Python List

• The list are most used datatype that are represented in square
braces [ ]. It looks similar like Array. But it is not. Array is static
and list is dynamic in size. Array has fixed datatype for all
elements, whereas list has no datatype bounding.
• A List also allows all 3 Operators like String.

• List1 = [ 25, 67, “abc”, 32.5, ‘xyz’, 5464e65]


Example:-

List1 = [ 25, 67, “abc”, 32.5, ‘xyz’, 5464e65 ]


List2 = [ 7854, “mno”, 76.9 ]
print(List1)
print(List1[0])
print(List1[-2:])
print(List1[-1])
print(List1[3:])
print(List1 * 2)
print(List1 + List2)
4. Python Tuple
• Tuple is another datatype which stores sequence of values,
similar to List. A tuple is also set of values separated by comma(,)
enclosed in parenthesis { ( ) }.

• List allows change/update its size and elements, whereas Tuple


does not allow updation in size or elements.

• A Tuple also allows all 3 Operators like String.


Example:-

Tuple1 = ( 25, 67, “abc”, 32.5, ‘xyz’, 5464e65 )


Tuple2 = ( 7854, “mno”, 76.9 )
print(Tuple1)
print(Tuple1[0])
print(Tuple1[-2:])
print(Tuple1[-1])
print(Tuple1[3:])
print(Tuple1 * 2)
print(Tuple1 + Tuple2)
5. Python Dictionary

• To work with List and Tuple we have to remember the index


numbers of element. The Dictionary solves this problem and allows
us to create our own index numbers for our elements.
• Dictionaries has pair of an index(key) and element(value).
• Its syntax is, { key : value }
• It can be any valid python data type. A dictionary is represented in
pair of curly braces ( { } ).
Example:-

dict = { “101” : “Rina” , “102” : “Lina” , “103” : “Tina” , “104” : “Sunita” }


print(dict)
print(dict.keys())
print(dict.values())

You might also like