Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
64 views

Python Notes

Python can be used as an interpreted, functional, object-oriented programming language. It has simple syntax and built-in libraries that allow for easier and faster development. Comments can be single-line using # or multi-line within triple quotes. Indentation using spaces is used to denote code blocks. Python supports a variety of data types including numeric, string, boolean, list, dictionary and more. Strings can be sliced, indexed, reversed, and manipulated using built-in functions.

Uploaded by

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

Python Notes

Python can be used as an interpreted, functional, object-oriented programming language. It has simple syntax and built-in libraries that allow for easier and faster development. Comments can be single-line using # or multi-line within triple quotes. Indentation using spaces is used to denote code blocks. Python supports a variety of data types including numeric, string, boolean, list, dictionary and more. Strings can be sliced, indexed, reversed, and manipulated using built-in functions.

Uploaded by

razzaque003
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

 Python is interpreted (no compilation required), functional, object-oriented language.

 It has simple syntax and inbuilt libraries for easier and faster compilation.
 Install python, Eclipse, Java and PyDev eclipse plugin to compile python scripts.
 For single line comments, use the # symbol at the beginning of the comment and for multi-
line comments, use either three double quotes or three single quotes at the beginning and
at the end.
 Indentation: The blank spaces before the block of code is called indentation. Its four spaces
before a particular line of code. It is used to separate blocks of codes.
 Data Types: It tells what kind of data a variable can carry. E.g. a=10 defines that a can carry
numeric data, f=10.23 means f can hold floating point data, s=”hello world” means s can
hold string of data.
Data Types in Python
 None: no data
 Numeric types: integer, floating point or complex data
 Sequences: strings, bytes, list, range etc.
 Sets: data that is not allowed to be duplicated
 Mappings: used to map and reduce
 Complex data examples:
A=5+3j
B=0b1010 (0b denotes binary value, so 0b1010 when executed will be equal to 10 in numeric)
C=0xFF (0x denotes hex value, so 0xFF when executed will be equal to 255 in numeric)
 Bool: it can carry two values, TRUE or FALSE.
 Data Type conversion: For example, to convert floating point data type to integer data type,
use the function int(x), where x is the floating point data. If x=10.23, then h=int(x) when
printed for the value of h will return a value of 10.
Similarly use the function float(x.y) to convert to float data type or bin function for binary
conversion, hex function for hexa conversion etc.
 Inbuilt data structures or collections in python:
 List: Any number of values or objects dynamically and maintain order of them
 Set: store any number of values dynamically but does not allow duplicates
 Dictionary: It’s a map used to store key and value pairs
 Indexing: Reaching out to a particular character or position in a string. We use [] to reach out
to the character position in the string. For example, if s=”hello world” then print(s) will print
hello world, print(s[0]) will print h and print(s[1]) will print e
 Repetition: to repeat a string multiple time. We use * and number of times we want to print
that string. For example, in above example, print(s*2) will print hello world 2 times.
 Length: used to find the number of characters in a particular string. For example,
print(len(s)) will return a value of 11 including the space character in between hello and
world. Index starts with 0 and goes upto len-1.
 Slicing: To slice a string. For example, print(s[0:3]) will print hel from hello world string. It
does not include the element at the ending index. Like here index 3 ends at the second l for
the string hello world but the print comes only upto first l. if you mention the starting index
as zero but do not mention the ending index print(s[0:]), it will print the whole string
starting from index 0. If you mention the starting index as 0 and end index as 3, it will start
printing from index 0 upto index 3. For example, print(s[0:3]) will print hel. Similarily,
print(s[:5]) will start printing from the beginning and will print hello.
 In negative indexing, -1 always represents the last character. For example, print s([-3:-1])
will print rl in hello world string.
 Steps in Slicing: In slicing, by default, the step jump is one. That is, the characters are counted
one by one. We can do slicing on desired steps like steps of 2 where alternate characters will
be counted instead of each character one by one. For example, print(s[0:7:2]) will print
characters starting from 0 upto 6 (last index is not printed) as usual but this time it will skip
every alternate character in between. In hello world example, the output would be “hlow”.
 Reversing a string: use print(s[::-1]). There is no start and end value. Only it starts from last
character.
 Strip: It is the function that removes any spaces before or after a string. Print(s.strip()). If you
wish to remove only the leading spaces, use the command lstrip like print(s.lstrip()). Similarily
for the right-hand side strip, use strip.
 Count: To count the number of occurrences of a particular character in a string, use the count
function. e.g.: print(s.count(l)) will return a value of 3 as l is repeated 3 times in string hello
world.
 Replace: To replace a character/characters with new character/characters.
e.g.: print(s.replace(“world”,”universe”)) will return hello universe in string hello world.
 Upper and lower: To print string in upper or lower case.

Import urllib.request

try:

url = urllib.request.urlopen(“https://www.python.org/”)

content = url.read()

url.close()

except urllib.error.HTTPError:

print(“The Web Page is Not Found”)

exit()

f = open(‘testpage.html’,’wb’)

f.write(content)

f.close()

You might also like