Tech Study

Python Examples

Introduction:

Python Examples are the basic programming concepts of python like python syntax,python data types,,python operators,python if else,python comments etc..

Here we will discuss about the commonly known python Examples.

Python Syntax:

The syntax of python can be directly executed in the command line.

print(“Hello, World!”)

Output: Hello, World!

Python Variables :

Unlike C and C++, here in python,variables are created when we  assign some value to them:

Ex:

x = 5

 y = 10

print(x)

print(y)

Output:

5

10

In p

Python Examples

In python , we dont need to declare the variables and there doesn’t exist any command for that.

Comments 

Python also facilitates us with its commoenting capability in the code.We can start the comment with # and the complete remaining  line is commented.

Ex:

#This is a comment. 

print("Hello, World!")

Python Examples:

Output: Hello, World!

Python datatype’s:

Python data types are used to specify  the type of a variable. It specifies what type of data is going to be stored in that variable..Python has many built-in data types.They are:

Numeric – int, float, complex

# integer variable. 

yy=90

 print("The datatype of ", yy, " is ", type(yy)) 

# float variable. 

x=2.45

 print("The datatype of ", x, " is ", type(x)) 

# complex variable.

 zz=3+4
print("The datatype of ", zz, " is ", type(zz))

 

Output:

The datatype of  90  is  <class ‘int’> 

The datatype of  2.45  is  <class ‘float’> 

The datatype of  (3+4j)  is  <class ‘complex’>

String – str

Ex:

s=”Python is fun!”

Python Examples

Boolean – bool

Ex:

a=True

b=False

 

Sequence – list, tuple, range

List: It contains a sequence of items similar to that of arrays in C and C++.They are separated by commas.

Ex of list:

list = [ 'cool', 7.6 , 23, 'prerna', 50 ]

Tuples:Tuples are read-only lists where the tuple elements and size is fixed.

tuple = ( 'cool', 7.6 , 23, 'prerna', 50 )

Mapping – dict

They are something like hashmaps and contain key-value pairs.

dict = {} 

dict['one'] = "This is one"

print (dict['one'])

Output:This is one

Python operators:Python operators are basic arithmetic operators like addition,subtraction,multiplication,division. etc..

Ex:

x = 10

y = 5

 print(x + y)

print(x - y)

print(x * y)

print(x / y)

print(x % y)

Output:

15

5

50

2

0

 

Python Examples

Java Final keyword

Introduction : java final keyword The final keyword present in Java programming language is generally used for restricting the user. …

Read more

C++ Memory Management: new and delete

C++ Memory Management We know that arrays store contiguous and the same type of memory blocks, so memory is allocated …

Read more