|
| 1 | +#assigning variable is ease in python |
| 2 | +#if we put some sort of value in any alphabet or word, without any indication it works as variable |
| 3 | +#eg |
| 4 | + |
| 5 | +x = "Nepal" |
| 6 | +Favourite_Hero = "Iron Man" |
| 7 | + |
| 8 | +print(x) |
| 9 | +print(Favourite_Hero) |
| 10 | + |
| 11 | +#naming rule |
| 12 | +#legal variables name |
| 13 | + |
| 14 | +myvar = 1 |
| 15 | +my_var = 2 |
| 16 | +_myvar = 3 |
| 17 | +MYVAR = 4 |
| 18 | +MyVar1 = 5 |
| 19 | + |
| 20 | +#illegal variables name |
| 21 | + |
| 22 | +# 2myvar = "a" my-var = "b" my var = "c" |
| 23 | + |
| 24 | +#single value to multiple variable and multiple variable |
| 25 | + |
| 26 | +x,y,z = 10,20,30 |
| 27 | +m =n =o ="Java" |
| 28 | + |
| 29 | +# print() statement prints the value stored in variable |
| 30 | +print(m) |
| 31 | +print(o) |
| 32 | +print(n) |
| 33 | + |
| 34 | +# + character is used to conccate variables |
| 35 | + |
| 36 | +a = "Ram" |
| 37 | +b = " and " |
| 38 | +c = "Shyam" |
| 39 | +print(a+b+c) |
| 40 | + |
| 41 | + |
| 42 | +#global variable and local variable |
| 43 | +#variable in global scope is global variable i.e not inside any function |
| 44 | +#variable inside a function is local variable and can ve used in that funcction only |
| 45 | + |
| 46 | +name = "harry" |
| 47 | + |
| 48 | +def function(): |
| 49 | + name1 = "garry" |
| 50 | + print (name1) |
| 51 | + print(name) |
| 52 | + |
| 53 | +#inside this function both name and name1 are printed |
| 54 | +print(name) |
| 55 | +#print(name1) |
| 56 | +#name1 cant be printed as it is not global variable |
| 57 | + |
| 58 | +#variable inside function can be made global variable using global keyword but function should be called before that. |
| 59 | +def function2(): |
| 60 | + global name2 |
| 61 | + name2 = "marry" |
| 62 | + |
| 63 | +function2() |
| 64 | +print(name2) |
0 commit comments