Exploring Basics Of Python
Python is an interpreted high level programming language . Python is used in web development, desktop application, software testing , data analysis, machine learning, artificial intelligence.
Data Types:
Python is rich in terms of data types. Data type is states that what type of data is and how one can use it . Data type is Python in categorized into two :
Python is rich in terms of data types. Data type is states that what type of data is and how one can use it . Data type is Python in categorized into two :
Mutable Data Type
It is a data type which content can be changed when required . Examples : Array , list , set , dictionary ,...
Immutable Data Type
It is a type of data which values can't be altered once it is defined . Examples : int , float, str, tuples,..
1.Integers
Like in math, integers in computer programming are whole numbers that can be positive, negative, or 0 (…, -1, 0, 1, …). An integer can also be known as an int.
Example:-
print(-2)
Output
-2
int_ans = 15 - 6
print(int_ans)
Output
9
2.Floating-Point Numbers
A floating-point number or a float is a real number, meaning that it can be either a rational or an irrational number.
Example:-
print(16.8)
Output
16.8
3.Booleans
The Boolean data type can be one of two values, either True or False.
Example:-
ex_bool = 18>12
print(ex_bool)
Output
True
4. Strings
A string is a sequence of one or more characters (letters, numbers, symbols) that can be either a constant or a variable. Strings exist within either single quotes ' or double quotes " in Python, so to create a string, enclose a sequence of characters in quotes.
hw = "Hello, World!"
print(hw)
Output
Hello, World!
Methods of Strings
There are various functions you can apply to strings that may be helpful .
- stringname.upper() - returns the string in upper case.
- stringname.lower() - returns the string in lower case.
- stringname.capitalize() - returns the string with the first letter of the string capitalized
- stringname.title() - returns the string with the first letter of each word capitalized.
- tringname.replace(x,y) - returns the string with the characters represented by x replaced by the characters represented by y.
- stringname[x:y] - returns the string starting at character x and ending before character y.
5.Lists
A list is a mutable, or changeable, ordered sequence of elements. Each element or value that is inside of a list is called an item. Just as strings are defined as characters between quotes, lists are defined by having values between square brackets [ ].
Example:-
[34,45,56,67,78]
name = ['Daisy', 'Joe', 'Fitz', 'Simmons']
print(name)
Output
['Daisy', 'Joe', 'Fitz', 'Simmons']
Methods of Lists
- append() - Add an element to the end of the list
- extend() - Add all elements of a list to the another list
- insert() - Insert an item at the defined index
- remove() - Remove an item from the list
- pop() - Remove and returns an element at the given index
- clear() - Removes all items from the list
- index() - Returns the index of the first matched item
- count() - Returns the count of number of items passed as an argument
- sort() - Sort items in a list in ascending order
- reverse() - Reverse the order of items in the listcopy() - Returns a shallow copy of the list
6.Tuples
A tuple is used for grouping data. It is an immutable, or unchangeable, ordered sequence of elements. Tuples are very similar to lists, but they use parentheses ( ) instead of square brackets and because they are immutable their values cannot be modified.
Example:-
('Krishna', 'Shyam', 'Rahul')
OR
Name= ('Krishna', 'Shyam', 'Rahul')
print(Name)
Ouput
('Krishna', 'Shyam', 'Rahul')
7.Dictionaries
The dictionary is Python’s built-in mapping type. This means that dictionaries map keys to values and these key-value pairs are a useful way to store data in Python. A dictionary is constructed with curly braces on either side { }.
Typically used to hold data that are related, such as the information contained in an ID, a dictionary looks like this:
{ 'name': 'Sunny', 'animal': 'whale', 'color': 'blue', 'location': 'ocean' }
The keys in the dictionary above are: 'name', 'animal', 'color', 'location'.
The words to the right of the colons are the values. Values can be comprised of any data type. The values in the dictionary above are: 'Sunny', 'whale', 'blue', 'ocean'.
Good going
ReplyDelete