DEV Community

Cover image for Day 1: Mastering the Basics of Python
Praneeth
Praneeth

Posted on

Day 1: Mastering the Basics of Python

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;
Enter fullscreen mode Exit fullscreen mode

here, the variable is declared as integer.

but in Python,

a = 6
Enter fullscreen mode Exit fullscreen mode

and the variable a can be redeclared as

a = "hello world"
Enter fullscreen mode Exit fullscreen mode

πŸ–‹οΈ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
Enter fullscreen mode Exit fullscreen mode
  • 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 
Enter fullscreen mode Exit fullscreen mode
  • In python, multiple variables can be declared in one line
a, b, c = 5, 6, 7
Enter fullscreen mode Exit fullscreen mode
  • Use the global keyword to modify a global variable inside a function
global x

def print():
  return x   #there will be no error
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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".

print documentation

example:

print("hello world")
print("hi")    # hello world
               # hi
Enter fullscreen mode Exit fullscreen mode
print("hello", "world", sep="-", end = " ")
print("hi")    # hello-world hi
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

πŸ–‹οΈ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
Enter fullscreen mode Exit fullscreen mode

In the upcoming blog, we'll delve into Python operators and conditional statements. Happy Learning😊😊😊

Top comments (0)