Data Types
Numeric value can be interger, floating number or even complex numbers. These values are defined as int, float and complex class in Python.
c = 1 + 1j print("Type of a, b, and c is " , type(2), type(1.5), type(c) , "respectively")
output: Type of a, b, and c is <class 'int'> <class 'float'> <class 'complex'> respectively
In Python, a sequence is the ordered collection of similar or different data types.
- String: A string is a collection of one or more characters put in a single quote, double-quote or triple quote. In python there is no character data type, a character is a string of length one. It is represented by str class. Strings in Python can be created using single quotes or double quotes or even triple quotes.
print('Welcome to the Python World', " I am a Pythonista", '''and I lives in world of "Python"''')
output: Welcome to the Python World I am a Pythonista and I lives in world of "Python"
String1 = "Pythonista"
print(String1[0])
print(String1[-1])
In Python, Updation or deletion of characters from a String is not allowed because Strings are immutable. Only new strings can be reassigned to the same name.
String1 = "Hello, I'm a Pythonista"
String1[2] = 'a'
Output: TypeError: ‘str’ object does not support item assignment
del String1[2]
Output: TypeError: ‘str’ object doesn’t support item deletion
Python slicing is about obtaining a sub-string from the given string by slicing it respectively from start to end. Python slicing can be done in two ways.
String ='Pythonista'
print(String[:3]) print(String[1:5:2]) print(String[-1:-12:-2])
print("\nReverse String") print(String[::-1])
Output: Pyt yh asnhy Reverse String atsinohtyP
Curious? okay please refer holybook of coding to know more https://www.geeksforgeeks.org/string-slicing-in-python/
Lists are just like the arrays, declared in other languages. A single list may contain DataTypes like Integers, Strings, as well as Objects. The elements in a list are indexed according to a definite sequence and the indexing of a list is done with 0 being the first index. It is represented by list class.
List = []
print(List)
List = ['Pythonista', 'Pythoneer'] print(List)
List = [['Python', 'For'], ['Beginners']]
print(List)
Adding Elements to a List: Using append(), insert() and extend()
List = []
List.append(1)
List.append(2)
print(List)
List.insert(3, 12)
List.insert(0, 'Geeks')
print(List)
List.extend([8, 'Geeks', 'Always'])
print(List)
List = [1, 2, 3, 4, 5, 6]
print(List[0])
print(List[2])
print(List[-1])
print(List[-3])