2. Variables in Python

 Variables: 

Variables are containers for storing data values. Unlike other programming languages, python has no command for declaring a variable. A variable is created at the moment you first assign a value to it. 
Python doesn't have any problem even if we don't declare the type of variable. It states the kind of variable in the runtime of the program. Python also takes care of memory management which is crucial in programming. So, Python is a dynamically typed language.
Rules for variables: 
1. Variables can't start with numbers. But can start with any letter. 
2. Special characters can't be used in the variable. 
3. If you use more than a single word in python, then separate them by _ 
E.g.   My_name_is =  Pankaj Kumar      # This type of writing is called snake writing. 

* String concatenation: 

It is a process of adding two strings together by using + operator. 
E.g. 
first_name = "Pankaj"
last_name = "Kumar"
full_name = first_name + last_name
print(full_name)

Output =  PankajKumar

* If you want to add space between your name. Then leave some space after double-quotes. "Pankaj  " 
or you can add a command for it full_name = first_name + "  " + last_name
* We can't add any number with string directly. But can be added using certain methods. Eg. 
print(full_name + 3)    #wrong
print(full_name + "3")    #Right
print(full_name + str(3) )   # Right   [Here str( ) is used for making anything as string. ]

We can't add any number with a string. But if we want to multiply the string for multiple times. Then we can use multiply symbols and print them.
E.g. print(full_name * 3)
Output =  Pankaj Pankaj Pankaj 

* User input: 

It is used to take input from the user using the input function. 
E.g.  name = input("type your name" )     # input is always taken in string. 
print ("hello" + name)

In the output, it will ask you to type your name & after typing the name. It will print hello + name. 
Eg. my name is Pankaj. I will get output,  hello Pankaj

# If I want to ask the user about his age. 
age = input ( "what is your age? ") 
print("Your age is " + age)

* Int( ) function: 

The int function convert specified value into integer number. 
E.g. 
number_one = input("enter the first number")  # first number is 4
number_two = input("enter the second number")  # second number is 4
total = number_one + number_two
print("total is " +  total)

Output:  total is 44.     # But we want to add these numbers.

Then we have to use the int ( ) function to convert these specified values into integer numbers. 

E.g. 
number_one = int(input("enter the first number")) # first number is 4
number_two = int(input("enter the second number")) # second number is 4
total = number_one + number_two 
print("Total number is " + str(total))

Output: 8
* str( )   :  4 ----->  "4" 
* int ( ) : "4" -----> 4
* float ( ) : "4" ------> 4.0 

* We can add int & float. But the final output will be in the float. 

* How to store multiple variables in a single line: 

E.g.   Name, Age = "Pankaj" , "18"
 print("Hello"  + Name + "your age is " + Age) 

* split( ) : 

split method convert a string into a list. 
E.g.  name = input("enter your name") 
age = input("enter your age") 

But if I want to write in a single line. Then I have to use the split function.
E.g. 
name,age = input("enter your name and age ") .split( )
print(name)    # when it ask to enter your name and age, then you have to type = pankaj 24
print(age)     # space is compulsory between name and age. Because it asked two values in input.

# string formatting: 

name = "Pankaj"
age = 18
print("hello" + name + "your age is " + "age") # we used such type of format for old versions of python. But in new version i.e. python 3. 
 
name ="Pankaj"
age = 18
print("hello {} your age is {} " .format(name, age))     # It this format, we have no need to care about the string or integer. It will automatically convert it as required.
 
& in python 3.6 : 
print( f " hello {name} your age is {age}")

* String Indexing: 

Index number started with 0,1,2,........
Eg. in Python:  P = 0, y = 1, t = 2, h = 3, o = 4, n = 5. 
But if want to print a digit from last i.e. like n,o. Then n = -1, o = -2, and so on . 
E.g. Input: 
language = "Python"
print(language[0])

Output: P
but if I write:  print(language[-1])
Then the output will be: n








2. Variables in Python 2. Variables in Python Reviewed by Endless Technology on 19:30 Rating: 5

No comments:

Ad Home

Powered by Blogger.