PYTHON BASICS SIMPLIFIED
PYTHON BASICS SIMPLIFIED
What is Python?
Python is a high level multipurpose scripting language used in web development, desktop application development, artificial intelligence, machine learning, game development, automation, tools development and a lot more.
Why Python?
- Easy Syntax & Readability
- Extensive standard library
- Large community
- Object oriented like any other efficient programming language.
- Many popular companies like Youtube, Instagram, Pinterest etc., use Python.
Getting Started
Installation:
Instructions available at
Getting familiar with basic syntax:
- Check basic python information on your machine such as python version using the command “python”.
- Standard output: Use the command “print” to print data to output. (use command “print()” from python3)
- Identifiers (Variables, Functions, Classes, Modules etc.,): Can be alphanumeric and are case-sensitive.
- Comments starts with a ‘#’ symbol.
Example: #This is a comment - Multi-line comments can be placed in between 3 quotes.
Example:
‘’’This is a
multiline comment
!’’’ - Strong Indentation:
Python will not use braces to define blocks like other object oriented languages.
So, it enforces very strict indentation to define blocks.
Example:
def func(a):
return a
TIP: Xcode for osX and PyScripter for windows are the simplest IDEs to begin with.
To practice straight away, logon to https://repl.it/languages/python
Python Variables
Below given are the python variable types.- Number : (Int, Long, Float, Complex)
- String: ‘’ or “ ” or “”” “””
- Tuple(): Allows all types of data types which applies to Lists too. Read only data type.
- List[]: List comprehension is one of the most useful concepts in Python along with Lambda expressions, RegEx
- Dictionary{}: unordered Key-value pairs.
- Type casting is done using the data type.
- Useful operators like floor division (//), exponent (**), IN and NOT IN, IS and IS NOT are available along with all other basic operators.
Conditionals and Loops
- Conditionals:
- If
- If..Else
- Nested If
- Loops: List Comprehension comes handy with loops
- For
- While
- Nested loops
Useful Functions
- Number Functions:
- String Functions:
capitalize(), count(x,S,E)
- List Functions:
max()
- Tuple Functions:
- Dictionary Functions:
d.has_key(key)
DateTime
Python has great libraries like “time”, “datetime”.
- Below code outputs a ‘structure’ as shown in the result
Import time
print(time.localtime(time.time())
#result:
time.struct_time(tm_year=2017, tm_mon=10, tm_mday=10, tm_hour=10, tm_min=43, tm_sec=3, tm_wday=1, tm_yday=283, tm_isdst=0)
- You can get a better format as below.
time.asctime(time.localtime(time.time()))
# -> Tue Oct 10 10:48:33 2017
- Breaking up time attributes:
import datetime
now = datetime.datetime.now()
print now.year, now.month, now.day, now.hour, now.minute, now.second
Result: 2017 10 10 10 57 38
Calendar
The “calendar” module helps to import calendar into our program. Given below is an example.import calendar
x = calendar.month(2017, 10)
print x
Printing calendar for complete year: x = calendar.calendar(2017)
Finding leap year: print calendar.isleap(2017)
Finding the weekday (0 to 6): print calendar.weekday(2017,10,22)
Functions
The best illustration of code reusability. Below shown is the syntax.def functionname(arg1,arg2,….):
<logic>
return [expression] #optional
functionname(arg1,arg2,….) #arguments can be a value or a reference
Example:
print "Even number generator"
limit=input("Enter the upper limit:")
even_list=[]
def eve(x):
for i in range(0,x):
if i%2==0:
even_list.append(i)
return even_list
print eve(limit)
Grt going sai ...keep it up🙂
ReplyDelete