As we all know python programming language has many similarities to other programming languages like C and Java. But there are also some key differences between these languages.
Your Very First Python Program
There are two ways in which you can execute python programs.
- Interactive Mode
- Scripting Mode
Execute your first program below!
Python Identifiers
In python programming language a name called identifier is used to identify a variable, class, function, a module or any other objects. There is proper rule for naming an identifier. Name of an identifier should start with a letter (A to Z or a to z) or an underscore (_) and this may be followed by zero or more letters,digits (0 to 9) or underscores.
Python programming language is case sensitive and it does not supports or allow any punctuation like @, $, and % in identifier names. Hence, Tutor and tutor are two absolutely different identifiers in Python Programming Language.
Now Lets talk about naming conventions for identifiers in Python-
- Most important if identifier name starts with two leading underscores and ends with two trailing underscores, then this identifier is language defined special name.
- And if identifier name has only two leading underscores then this means that it is a strongly private identifier.
- And if identifier name has only one (single) leading underscore then this means that it is a private identifier.
- All identifiers start with a lowercase letter whereas a Class name starts with an Uppercase letter.
Reserve Words in Python
Here are the words that are reserved in python programming language, it means that these words can not be used as any other variable or identifier names. Keep one thing in mind that all keywords contains only lowercase letters.
not | and | exec |
or | assert | finally |
pass | break | for |
class | from | |
raise | continue | global |
return | def | if |
try | del | import |
while | elif | in |
with | else | is |
yield | except | lambda |
Indentation
As other programming language like C++ Python does not provide any braces to indicate any block of code. Here in python programming language blocks of code are shown by Line Indentation.
In indentation number of spaces can be variable but all the following statements within a particular block must be indented with same amount of white spaces. As for example:
if a == 1:
print("Value of a is 1")
else:
print("Value of a is not 1")
Above written code will not show any error because it has been indented properly. But the code shown below will return error statement-
if a == 1:
print "Value of a is"
print("1")
else:
print "Value of a is not"
print("1")
Thus we can say that in Python Programming Language all simultaneous lines that are indented with same number of white spaces will be considered in same block.
For understanding following is a code, Just try to understand indentation not logic behind the code:
#Source Link: https://github.com/kaish114/CoolKit/blob/master/code1.py
import requests
import bs4
debug = True
if(not debug):
req = requests.get("http://codeforces.com/contests")
soup = bs4.BeautifulSoup(req.text,"html.parser")
else:
soup = bs4.BeautifulSoup(open("codeforces1.html"),"html.parser")
a= soup.find_all(id='body')
b=a[0].find_all('div',recursive=False)
c=b[3].find_all('div',recursive=False)
d=c[1].find_all('div',recursive=False)
constestList = d[0]
datatable,contesttable = constestList.find_all('div',recursive=False)
#all we need is datatable
table_div=datatable.find_all('div',recursive=False)
table = table_div[5].find_all('table',recursive=False)
if(debug):
tbody = table[0].find_all('tbody',recursive=False)[0]
upcoming_contest = tbody.find_all('tr',recursive=False)[1:]
else:
upcoming_contest=table[0].find_all('tr',recursive=False)[1:]
print("number of upcoming contests is",len(upcoming_contest))
#hurray we did it
for contest in upcoming_contest:
name_of_contest = contest.find_all('td',recursive=False)[0]
contest_id = contest.get('data-contestid')
print(" ",contest_id," : ",name_of_contest.get_text()[1:])
print("select one contest you want to participate : ",end='')
contest_id = str(input())
link = "www.codeforces.com/contest/"+contest_id+""
print(link)
Multi-Line Statements in Python
Generally in python a new line ends with a new line. But in Python you can write multi-line statements with continue character (\). It denotes that the line will continue to next line. As for example-
#Multi-line Statement Example
total_price = apple_price + \
orange_price + \
banana_price
#However statements within [], {} or () do not need any continuation character
total_years = ['2015', '2016', '2017',
'2018', '2019', '2020']
Quotations
To denotes strings in Python quotations are used. It uses single (‘), double (“) and also triple (”’ or “””) quotations but you should make sure to use same type of quote in start and end of that string.
As we discussed above continue character is used to write multi-line statement same as triple quotes are used to span to multiple lines. As for example-
name = 'The Engineer Guy'
about = "The Engineer Guy is all about Data Science."
more_about = """You can use triple quotes to span
sentences to multiple line."""
Comments in Python Programming
The most useful feature of any programming language is comment. You can use comments to explain your code to others who are not familiar to your code. In python programming language hash symbol(#) is used. All the symbols and characters after # comes under comment in the same line and python interpreter ignores them during execution.
#Comment
# this line is also comment
print('Hello TEG!') #This is also comment
Above code will produce-
Hello TEG!
Triple-quote can be used for multi-line comment as shown below-
'''
This triple quote is used for
multi-line comment.
'''