Posts

Showing posts from February, 2018

List Data Type in Python

Image
Concept List  is a collection which is mutable, ordered and changeable. Allows duplicate members. List are just like the arrays, declared in other languages. List can contain int, float, str, list, dict, set any data type. Code Example Example 1: # empty list >>> l = [] [] Example 2: >>> l = [10, 20.35, 'Hello'] [10, 20.35, 'Hello'] Example 3: >>> l = [10, 20.35, 'Hello'] >>> len(l) 3 >>> l [1] 20.35 # append >>>  l.append("New") [10, 20.35, 'Hello', 'New'] >>  l [1: 3] [20.35, 'Hello'] >>> l [-3, -1] 20.35, 'Hello'] >>> l [0] = 40 >>> l [40, 20.35, 'Hello', 'New'] #  reverse >>> l.reverse() >>> l ['New', 'Hello', 20.35, 40] #  extend >>> l = ['New', 'Hello', 20.35, 10] >>> l1 = ['a', 'b'] >>> l.extend(l1) >>> l ['N

String Import in Python

Image
Example The string is one of the popular modules in python. We can deal with Uppercase Letters, Lowercase Letters, Numeric etc. See the below Code........ Example 1: >>> import string >>> string.ascii_letters 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' Example 2: >>> string.ascii_lowercase 'abcdefghijklmnopqrstuvwxyz' >>> string.ascii_uppercase 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' Example 3: >>> string.hexdigits '0123456789abcdefABCDEF' >>> string.digits '0123456789' # chr()  this function takes ascii code return symbol Example 4: >>> for i in range(40, 100): print(chr(i)) ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c Enjoy String Import Tutorial 😉 Get Help

Python Escape Characters

Image
   Concept To insert characters that are illegal in a string, use an escape character. An escape character is a backslash \  followed by the character you want to insert. Suppose you want to write Python's Blog then you need an escape character. Code >>> print('I like tea \ coffee') SyntaxError: invalid syntax To solve this issue we need to put one '\' over there and it will work as an escape character. Example: >>> print('Python\'s Blog') Python's Blog >>> print('I like tea \\ coffee' ) I like tea \ coffee >>> print('I Love \" Pythom \" ') I Love " Pythom "  Get Help

String Formatting in Python

Image
  Concept In Python below code will create an 'Type Error' because python does not support this kind of operation. age = 36 txt = "My name is John, I am " + age print(txt)  Output: TypeError: must be str, not int So get rid of this issue we basically do ' String Formatting ' by using format() method.   Code Example 1: txt = "I love {} & {}" print(txt.format('Python', 'Django')) Output: I love Python & Django   Example 2: txt = "I love {0} & {1}" print(txt.format('Python', 'Django')) Output: I love Python & Django   Example 3: txt = "I love {1} & {0}" print(txt.format('Python', 'Django')) Output: I love Django & Python   Example 4: txt = "I love {x} & {y}" print(txt.format(x='Python', y='Django')) Output: I love Python & Django Get Help

Python String Data Type

Image
  Example String is one the datatype for python. String literals in python are surrounded by either single quotation marks, or double quotation marks. 'hello'  is the same as  "hello". Python Support positive as well as Negative Indexing var = "Hello" is same as var = 'Hello' var = "I Love Python" # Multiline String var = ''' I Love Python I Love Django '''' ''' ''' ---> this one is known as docstring in python This is How Python store a string, see the below Image a =  "Hello, World!" print (a[ 0 ]) Output: H a =  "Hello, World!" print (a[1 ]) Output: e a =  "Hello, World!" print (a[- 1 ]) Output: ! a =  "Hello, World!" print (a[ -2 ]) Output: d # Python Slice Concept # Remember it touches Lower bound not Does not touch the upper bound. a =  "Hello, World!" print (a[ 0: 3 ]) Output: Hel a =  "Hello, World!" print (a[ 2: 3 ]) O