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