Home > Backend Development > Python Tutorial > Day Mastering the Basics of Python

Day Mastering the Basics of Python

DDD
Release: 2024-12-04 18:00:24
Original
425 people have browsed it

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;
Copy after login
Copy after login

here, the variable is declared as integer.

but in Python,

a = 6
Copy after login
Copy after login

and the variable a can be redeclared as

a = "hello world"
Copy after login

?️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
Copy after login
  • 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 
Copy after login
  • In python, multiple variables can be declared in one line
a, b, c = 5, 6, 7
Copy after login
  • Use the global keyword to modify a global variable inside a function
global x

def print():
  return x   #there will be no error
Copy after login
  • 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
Copy after login

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
Copy after login

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

Day Mastering the Basics of Python

example:

print("hello world")
print("hi")    # hello world
               # hi
Copy after login
print("hello", "world", sep="-", end = " ")
print("hi")    # hello-world hi
Copy after login

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
Copy after login
  • 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
Copy after login

input()

The input() function in Python is used to take user input from the console.

int a = 6;
Copy after login
Copy after login

?️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 = 6
Copy after login
Copy after login

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

The above is the detailed content of Day Mastering the Basics of Python. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template