DEV Community

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

Posted on

2

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😊😊😊

API Trace View

How I Cut 22.3 Seconds Off an API Call with Sentry πŸ•’

Struggling with slow API calls? Dan Mindru walks through how he used Sentry's new Trace View feature to shave off 22.3 seconds from an API call.

Get a practical walkthrough of how to identify bottlenecks, split tasks into multiple parallel tasks, identify slow AI model calls, and more.

Read more β†’

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

πŸ‘₯ Ideal for solo developers, teams, and cross-company projects

Learn more

πŸ‘‹ Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay