For the sake of simplicity, I am dividing this into 3 parts:
- Variable Declaration
- Taking input and Declaring Output
- Operators and Expressions
1.Variable Declaration
Unlike other programming languages like c, cpp and java ,you donβt need to declare the type of a variable explicitly. This feature is called Dynamic Typing.
in C,
int a = 6;
here, the variable is declared as integer.
but in Python,
a = 6
and the variable a can be redeclared as
a = "hello world"
ποΈNOTE:
Variable names are case-sensitive., so a and A are considered different variables.
β¨ Features and Rules In Declaration
- Variable names must begin with an alphabet or an underscore (_)
a = 6 #valid
_a = 6 #valid
-a = 6 #invalid
- The "_" variable stores the result of the last expression in interactive Python mode. This can be visible in Jupiter Notebook
a = 5
b = 6
a + b #11
print(_) #11
- In python, multiple variables can be declared in one line
a, b, c = 5, 6, 7
- Use the
global
keyword to modify a global variable inside a function
global x
def print():
return x #there will be no error
- Unlike c and cpp ,Python does not have a built-in way to declare constants. By convention, variable names in all caps are treated as constants.
PI=3.14
2.Taking input and Declaring Output
print()
The print() function is a built-in Python function used to display output to the console.
a=10000
print("hello world") #hello world
print("hello", "world")#hello world
print("hello world",a) #hello world 10000
In print function, there are two main parameters.
- sep : It determines how multiple objects are separated when printed. It is usually preset to " ".
- end : It defines what is printed at the end of the output.It is usually preset to "\n".
example:
print("hello world")
print("hi") # hello world
# hi
print("hello", "world", sep="-", end = " ")
print("hi") # hello-world hi
for the complete documentation of print, click here
β¨ Features of Print()
- You can use escape sequences (e.g., \n for a newline, \t for a tab, and _\_ for a backslash) to include special characters in the printed output.
print("hello\nworld") #hello
#world
print("hello\tworld") #hello world
print("happy\\trecking")#happy\trecking
- print() is often used for debugging and tracing code execution because it provides a quick way to output variable values and program state.
x = 5
y = 10
print(f"x: {x}, y: {y}") # x: 5, y: 10
input()
The input() function in Python is used to take user input from the console.
name = input("Enter your age: ")
print("your age is: ",name)
# If the user enters 18, the output will be: "your age is 18"
ποΈNOTE:
By default, input() returns a string, so if you need to use the input as a different type (e.g., int, float), you need to convert it.
a =input("enter your age: )
type(a) #str
a=int(input("enter your age: ))
type(a) #int
In the upcoming blog, we'll delve into Python operators and conditional statements. Happy Learningπππ
Top comments (0)