Python Basic
Python Basic
Python Basic
Suman Halder
Key features
Python has many reasons for being popular and in demand. A few of the
reasons are mentioned below.
• Emphasis on code readability, shorter codes, ease of writing.
• Programmers can express logical concepts in fewer lines of code in
comparison to languages such as C++ or Java.
• Python supports multiple programming paradigms, like object-oriented,
imperative and functional programming or procedural.
• It provides extensive support libraries(Django for web development, Pandas
for data analytics etc)
• Dynamically typed language(Data type is based on value assigned)
• Philosophy is “Simplicity is the best”.
Application Areas
1) Printing hello world--> print("Hello World") [no need of ;]
x = 5;y=6
print( "x =%d y=%d"%(x,y) )
2) Python Indentation-->
Python uses indentation to highlight the blocks of code. Whitespace is used for indentation in Python. All statements
with the same distance to the right belong to the same block of code. If a block has to be more deeply nested, it is
simply indented further to the right. You can understand it better by looking at the following lines of code.
1)capitalize-->
Upper case the first letter in this sentence:
txt = "hello, and welcome to my world."
x = txt.capitalize()
print (x)
2) casefold-->
#Make the string lower case:
txt = "Hello, And Welcome To My World!"
x = txt.casefold()
print(x)
3) center-->
#Print the word "banana", taking up the space of 20 characters, with "banana" in
the middle:
txt = "banana"
x = txt.center(20)
print(x)
4)count-->
#Return the number of times the value "apple" appears in the string:
txt = "I love apples, apple are my favorite fruit"
x = txt.count("apple")
print(x)
5) endswith-->
#Check if the string ends with a punctuation sign (.):
txt = "Hello, welcome to my world."
x = txt.endswith(".")
print(x)
6) find-->
#Where in the text is the word "welcome"?:
txt = "Hello, welcome to my world."
x = txt.find("welcome")
print(x)
[throws –1 not found case]
7)index-->
#Where in the text is the word "welcome"?:
txt = "Hello, welcome to my world."
x = txt.index("welcome")
print(x)
[throws exception not found case ]
8)format-->
#Using different placeholder values:
print("My name is {fname}, I'am {age}".format(fname = "John", age = 36))
print("My name is {0}, I'am {1}".format("John",36))
print("My name is {}, I'am {}".format("John",36))
9) isalnum-->
#Check if all the characters in the text are alphanumeric:
txt = "Company12"
x = txt.isalnum()
print(x)
10) isalpha-->
#Check if all the characters in the text are letters:
txt = "CompanyX"
x = txt.isalpha()
print(x)
11) isdigit-->
#Check if all the characters in the text are digits:
txt = "50800"
x = txt.isdigit()
print(x)
isdigit() Returns True if all characters in the string are digits
isidentifier() Returns True if the string is an identifier
islower() Returns True if all characters in the string are lower case
There are four collection data types in the Python programming language:
• List is a collection which is ordered and changeable. Allows duplicate
members.
• Tuple is a collection which is ordered and unchangeable. Allows duplicate
members.
• Set is a collection which is unordered and unindexed. No duplicate
members.
• Dictionary is a collection which is unordered, changeable and indexed. No
duplicate members.
When choosing a collection type, it is useful to understand the properties
of that type. Choosing the right type for a particular data set could mean
retention of meaning, and, it could mean an increase in efficiency or
security.
LISTS
thislist = ["apple", "banana", "cherry"]
print(thislist[1]) --> print 2nd item
2) print(thislist[-1]) --> print last item
3) thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[2:5]) --> Return the third, fourth, and fifth item
4) print(thislist[:4]) --> This example returns the items from the beginning to "orange"
5) print(thislist[2:]) --> This example returns the items from "cherry" and to the end:
6) print(thislist[-4:-1])--> This example returns the items from index -4 (included) to index
-1 (excluded)
7) thislist[1] = "blackcurrant"
8) print(len(thislist)) --> Print the number of items in the list:
9) thislist.append("orange")--> Using the append() method to append an item
10) thislist.insert(1, "orange") --> To add an item at the specified index, use
the insert() method:
11) thislist.remove("banana")
12) thislist.pop() --> The pop() method removes the specified index, (or the last
item if index is not specified):
13) del thislist[0]--> The del keyword removes the specified index:
14) list1.extend(list2) --> Use the extend() method to add list2 at the end of list1:
15) fruits.reverse() --> Reverse the order of the list:
16) sorted(iterable, key=key, reverse=reverse)
a = ("h", "b","c","g", "a", "c", "f", "d", "e", "g","g")
x = sorted(a, key=a.count,reverse=True)
print(x[:1])
17) list.sort(reverse=True|False, key=myFunc)
# A function that returns the length of the value:
def myFunc(e):
return len(e)
cars = ['Ford', 'Mitsubishi', 'BMW', 'VW']
cars.sort(reverse=True, key=myFunc)
FILE
"r" - Read - Default value. Opens a file for reading, error if the file does not exist
"a" - Append - Opens a file for appending, creates the file if it does not exist
"w" - Write - Opens a file for writing, creates the file if it does not exist
"x" - Create - Creates the specified file, returns an error if the file exists