Variable and Data Types in Python


Variables in Python

A variable is a named storage location where you can save some value when you execute your code. It takes some space in your RAM and its value persists till the time your code is executing.

E.g

# Without variables 
# Add two numbers
print(10+20)
#With variables
a=20
b=20

print(a+b)

The benefit of saving your values to a variable is that the values and calculation (addition in our case addition) can be reused in some further parts of the code.




Comments

A comment is a line in your code that you don’t want to run but write them just for some reference in your code like explaining what your code is doing.

E.g

#this line do addition

c=a+b


In python, comments can be done using a ‘#’ symbol in the start of the line. It will inform the interpreter not to execute that line.


Data Types in Python

A data type in python refers to different formats of data that we can use in python. We are not always working with the same type of data. For example, to store the details of a student I will need different values like 

Name: ”Tarun”

Age: 18

Percentage: 89.5

Subjects: [‘Maths’,”English”,’Hindi’,’Sci’,’SST’]

In this above example, we can clearly see that the data with which we want to work is in different formats like the name is a string type, age is an int type, the percentage is a float type and subjects is of type list.

Following are the different data types supported by python:

  • Integers

  • Float

  • String

  • List

  • Dictionary

  • Boolean

  • Set

  • Tuple




int (Integers)

An int data type is used to save a whole number. It does not support numbers with decimal places. There are two ways in which you can create an int variable

SYNTAX:

  1. <variable-name>=int(<value>)

  2. <variable-name>=<value> #value is a whole number





float (Decimal numbers)

A float data type is used to save numbers with decimal values like 10.2, 52.223. There are two ways in which you can create a float variable

SYNTAX:

  1. <variable-name>=float(<value>)

  2. <variable-name>=<value> #value is a decimal number





str(Strings)

A string data type is used to save the values of type strings like names, addresses, etc. In python, anything enclosed in single quotes (‘) of (“) is considered as a string.

SYNTAX:

  1. <variable name>=str(“<value>”)

  2. <variable name>=str(‘<value>’)

  3. <variable name>=“<value>”

  4. <variable name>=“<value>”upper()
    Converts lowercase letters in string to uppercase


Strings are internally treated as a list in python (List data types are covered next). Strings various functions in which are pre-defined and can be used in the python code. Some of the functions are as follows:


Capitalize ()

Capitalizes the first letter of the string


Count (str, beg= 0, end=len (string))

Counts how many times str occurs in a string or in a substring of string if starting index beg and ending index end are given.


endswith (suffix, beg=0, end=len (string))

Determines if a string or a substring of string (if starting index beg and ending index end are given) ends with a suffix; returns true if so and false otherwise.


find (str, beg=0 end=len (string))

Determine if str occurs in a string or in a substring of string if starting index beg and ending index end are given returns index if found and -1 otherwise


index (str, beg=0, end=len (string))

Same as find (), but raises an exception if str not found


isalnum ()

Returns true if the string has at least 1 character and all characters are alphanumeric and false otherwise


isalpha()

Returns true if the string has at least 1 character and all characters are alphabetic and false otherwise


isdigit()

Returns true if the string contains only digits and false otherwise


islower()

Returns true if the string has at least 1 cased character and all cased characters are in lowercase and false otherwise



isnumeric()

Returns true if a Unicode string contains only numeric characters and false otherwise


isspace()

Returns true if the string contains only whitespace characters and false otherwise.


istitle()

Returns true if the string is properly "title cased" and false otherwise


isupper()

Returns true if the string has at least one cased character and all cased characters are in uppercase and false otherwise


join(seq)

Merges (concatenates) the string representations of elements in sequence seq into a string, with separator string


len(string)

Returns the length of the string


lower()

Converts all uppercase letters in a string to lowercase


lstrip()

Removes all leading whitespace in string


replace(old, new [, max])

Replaces all occurrences of old in a string with new or at most max occurrences if max given


split(str="", num=string.count(str))

Splits string according to delimiter str (space if not provided) and returns list of substrings; split into at most num substrings if given



startswith(str, beg=0,end=len(string))

Determines if a string or a substring of string (if starting index beg and ending index end are given) starts with substring str; returns true if so and false otherwise


strip([chars])

Performs both lstrip() and rstrip() on string


title()

Returns "titlecased" version of the string, that is, all words begin with uppercase and the rest are lowercase


upper()

Converts lowercase letters in a string to uppercase



In the Next Blog, we will know about the List, Set, Dictionary and Tuple Data Types.

If you still have any doubt watch the video:


Thanks For Learning with me and subscribe for learning more and get notifications for new blogs and videos:

Blog link: TechScouter

Channel link: The Data Singh


Comments

  1. Thankyou for such informative blog about python. Python Training Course in Greater Noida is the best place to start career as an python developer.

    ReplyDelete

Post a Comment

Popular posts from this blog

Spidering the web with Python

Word Vectorization

Machine Learning -Solution or Problem