Python – Change data types | Type Conversion

Python defines type conversion functions to directly convert one data type to another which is useful in day to day and competitive programming. This article is aimed at providing the information about certain conversion functions.
  • int(a,base) : This function converts any data type to integer. ‘Base’ specifies the base in which string is if data type is string.
  • float() : This function is used to convert any data type to a floating point number

Note:

[highlight bgcolor=”#000000″ txtcolor=”#eeee22″]

str + int→ Error

str * str→ Error

str + str→ str

str * int→ strstrstr… int multiple

[/highlight][highlight bgcolor=”#000000″ txtcolor=”#eeee22″][/highlight]

Example

print("5" + "5")
print((int("5") + int("5")) * 10)
print(int("5") + int("5") * 10)
print(int("5" + "5") * 10 )
print(("5" + "5") * 10 )

output :

55
100
55
550
55555555555555555555

 

Hits: 100

Python – Strings Operations with Example’s

In Python Strings can be created with single or double quote it does’nt matters . where as “hello”+”world” will be print as helloworld  (no gap)   and “hello”,”worlds will be print as hello world (with gap)

if you add operator between stings it will add the both strings which is called  String Concatenation

Example 1 

print(“good” + “morning”)
output : goodmorning

If your strings contains no.  integer values it will be added  using (+) sign will add numbers

Example 2

print( 5 +6)

output :  11

print (“5″+”6”)

output : 56

Note : You cant add string type to the numeric type

Example :

print (5 +”6″)

output : Traceback (most recent call last): File “..\Playground\”, line 1, in <module> \ufeffprint( 5 +”6″) TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’

In Python Strings can also be multiplied.

Srings can be multiplied with integer but not with strings or float

 

Example 3

print(“hello” * 5)

output : hellohellohellohellohello

print (hello * hello)

output : Traceback (most recent call last): File “..\Playground\”, line 1, in <module> \ufeffprint(hello * hello)

 

Hits: 91

Python | Basic Input and Output Functions

The Basic input and output functions are most confusing in Python Language  ,The print and input functions aren’t very useful at the Python console.

Here is the Simple Example


input("Enter Something")

Store input in Variable


s = input("Enter something: ")
print(s)


#enter hello world

Output : hello world

Examples

a=input("Enter your name:")
print(a)
b=input("Enter your age:")
print(b)

output :
Enter your name :_______
Enter your age:______

you just have to enter your answer

Hits: 72

Python – Strings,Assign String to a Variable,Multiline Strings

To Enter a string in python Print function is used . Strings can be created by entering text between single quotation mark or double quotation mark.

string is a sequence of characters that are not numeric.

String using Single or double quote

For Example

class="auth">#Author : Paras Guglani
#Website : Passionategeekz.com
#created : 27/06/2019 13:42pm

print ("hello world")
print ('hello world')

hello world
hello world
[Finished in 0.3s]

Note : Both the Result would be same either in single quote or double quote

 

Assign Variable  To String

You can also Assign a Variable to string . Enter name of variable followed by equal sign and the single or double quote string

class="auth">
#Author : Paras Guglani
#Website : Passionategeekz.com
#created : 27/06/2019 13:42pm

a= 'hello\n'
b= "world"
print (a+b)

hello
world
[Finished in 0.3s]

MultiLine Strings

In Python we can also use a multi line string using three double quotes “””your string”””

#Author : Paras Guglani
#Website : Passionategeekz.com
#created : 27/06/2019 13:42pm

a= """hello this is
my first python program using a
multi line strings"""
print (a)

hello this is
my first python program using a
multi line strings
[Finished in 0.3s]

Hits: 123

Python -Exponentiation (Power of a number)

Python also supports math functions like find a power of  number , in this program we will learn how to find a power of number using exponential operator (**)

WRITE A PROGRAM TO FIND A TO THE POWER B  IN WHICH  A=2 , AND B=4 BY MATHS WE KNOW 2  RAISE TO POWER 4 WOULD BE  16

LETS FIND THIS USING PROGRAM PYTHON

#Author : Paras Guglani
#Website : Passionategeekz.com
#created : 23/06/2019 13:53PM

a=2
b=4
c=(a**b)
print(c)

16
[Finished in 0.3s]

Hits: 85

Python-Float Decimal places

Python used Integers as Float no.’s for example if we divide two no. such as 10/2 output would be 5.0 instead of 5 , dividing any two integers produces a float.

Converting an integer into a float makes the operation more accurate, that’s why python is used in scientific computing

Dividing ALWAYS results in a float,Even if divided perfectly

Computers can’t store reoccurring decimals perfectly.

#Author : Paras Guglani
#Website : Passionategeekz.com
#created : 23/06/2019 00:47am

a=10
b=5
c=(a/b)
print(c)

2.0
[Finished in 0.3s]

Note: We don’t have to declare int or float

 

Hits: 28

Python-Program For Basic Arithmetic Operations

In this program , we will show how to Perform arithmetic operations  such as addition,subtraction,multiplication and division in Python with numeric values

#Author : Paras Guglani
#Website : Passionategeekz.com
#created : 22/06/2019 17:03pm

Here is Code for this python program here we used direct numeric values to perform operations

print(6+6)
print(6-6)
print(6*6)
print(6/6)

12
0
36
1.0
[Finished in 0.1s]

 Note: Python does not  follows the BODMAS Rule , order of execution left to right of operator is matters

Hits: 16

What is Python? Why is it named Python

What is Python

Python is a good programming language for beginners. It is a high-level language, which means a programmer can focus on what to do instead of how to do it. Writing programs in Python takes less time than in another language.
It is very popular and used by organizations such as Google, NASA, the CIA, and Disney.There is no need to compile your program before executing it.

Why is it named Python

When he began implementing Python, Guido van Rossum (Python inventor) was also reading the published scripts from “Monty Python’s Flying Circus”, a BBC comedy series from the 1970s. Van Rossum thought he needed a name that was short, unique, and slightly mysterious, so he decided to call the language Python.
Python has several libraries for different purposes like

  • Pygame for game development
  • Pyglet for 3D
  • Djanngo(a framework) for websites

Hits: 51