Python (Variables)

2022. 1. 2. 10:57Python/Python 변수 (Variables)

반응형

Python (Variables)

(Variable) is the name of the space used to store data in memory.

 
 

Data or objects stored in variables can change continuously within a program.

Learn how to declare and use variables.

The sequence is as follows.

 

Variable declaration and assignment

In Python, declaration and assignment of variables occurs simultaneously.

declaration and assignment

age = 10

print(age)
print(type(age))
10
<class 'int'>

We assigned the number 10 to a variable named age.

In Python, the data type of a variable is automatically determined according to its value without specifying the data type.

If you check the data type of a variable using the Python built-in function type(), you can see that it is an int type.

 
 

assigning new values

age = 10
print(age)

age += 1
print(age)
10
11

A variable's value remains the same as long as a new value is not assigned within the code.

If you add 1 to a variable using the assignment operator +=, the value of the variable is now 11.

 

 
 

various data types

age = 11
deposit = -33000
ratio = 1.5
fruit = 'apple'

print(age, type(age))
print(deposit, type(deposit))
print(ratio, type(ratio))
print(fruit, type(fruit))
11 <class 'int'>
-33000 <class 'int'>
1.5 <class 'float'>
apple <class 'str'>

Variables can be assigned different values, such as integers, real numbers, and strings.

If you check the data types, you can see that they are int, float, str, respectively.

 
 

string concatenation

age = 11 + 1
fruit = 'pine' + 'apple'

print(age)
print(fruit)
12
pineapple

When numbers are added together, the value is added.

Adding strings concatenates strings.

This feature of Python is called string concatenation.

 

 
 

Declare and assign at the same time

a = b = c = 10
d, e, f = 10, 35.2, 'pineapple'

Variables with the same value can be declared at the same time,

You can also assign different values ​​to each at the same time.

 
 
 

Naming Variables

An introduction to naming variables in Python code.

 

1. Let us understand the meaning.

a = 10               # Bad
age = 10             # Good

For example, it is better to name a variable representing age ‘age’ instead of ‘a’.

 

 
 

2. Avoid lowercase ‘l’/uppercase ‘O’/uppercase ‘I’.

l = '1ong'           # Bad!
O = '0range'         # Bad!
I = 'line'           # Bad!

The lowercase l can be difficult to distinguish from the number 1.

The uppercase O can be difficult to distinguish from the number zero.

The uppercase I can be difficult to distinguish from the lowercase l.

 
 

3. Names of variables and functions in lowercase.

def hello():
   print('Hello')

hello()

Variables and functions are named in lowercase by default.

 

 
 

4. Use underscore (_) for string concatenation.

To improve readability, connections between words use underscores (_).

function

def say_hello():
   print('Hello')


say_hello()

variable

 

 

fruitsnumber = 20    # Bad
num_fruits = 20      # Good
 
 

5. Name the class in CapWords style.

class MemberInfo():
   pass

When naming classes, capitalize only the first letter of words, such as CapWords.

 

 
 

6. Constants are all uppercase.

PI = 3.14
TEMP = 300

Use all uppercase letters for constants used in Python.

 

 

 

If the material was helpful, please subscribe and like it on YouTube Onya TV below!

https://www.youtube.com/channel/UCKEjO6sQJjlyzbHGeqd3hKg

 

ONYA TV 오냐TV

오늘의 인터뷰 오냐TV 입니다. 벤처 또는 신규 기업, 새롭고 다양한 것을 생생하게 인터뷰 하는 오냐TV 채널 입니다. 새로운 상품이나 서비스 등 소개를 '유튜브'에서 전달하고 있고, "오냐TV" 영상

www.youtube.com

 

There is a small honey gain income as passive income, so please check the contents below as well~^^
(Refers do not receive $5, so be sure to click on it for your own benefit.)

https://r.honeygain.money/OLIVE47A16

 

Passive Income - Effortlessly | Honeygain

Honeygain is the first-ever app that allows users to make money online by sharing their internet connection.

www.honeygain.com

 

Copyright 2021. beauty886699.tistory.com

반응형