0% found this document useful (0 votes)
76 views

Python Variables and Operator

This document discusses variables, operations, and naming conventions in Python. It shows examples of simple math operations, strings, comments, and variable definition. It also demonstrates implicit and explicit type conversions, in-place operators, and some Pythonic programming idioms like line continuation and swapping variables. The document is from a Python course and intends to teach Python basics and coding style guidelines.

Uploaded by

mansour
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

Python Variables and Operator

This document discusses variables, operations, and naming conventions in Python. It shows examples of simple math operations, strings, comments, and variable definition. It also demonstrates implicit and explicit type conversions, in-place operators, and some Pythonic programming idioms like line continuation and swapping variables. The document is from a Python course and intends to teach Python basics and coding style guidelines.

Uploaded by

mansour
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Variables and Operations

# Simple Operations ----------------- _privateVar = 23.0 # Private


print(3 + 4) Variable or Function [Weakly Private]
print(10 / 5) # Output is float __VeryPrivateVar = 4.3 # Very Private!
print(3.0) # float define Variable
print(3 * 3.0) # float output # Special variables: Magic Methods
print(1 - 1.0) # float output (Like Macro in C/C++)
print(4 ** 2)
__MANS__ = 20.0
print((4 ** 2) ** 3)
print(4 ** 2 ** 3) # Taking the user input -------------
# A = input()
# Other Operations # A is a string
print(13 // 5) # floor division # A1 = input("Enter some value: \n")
print(13 % 5) # remainder
# print(3 * input()) # Short Code Style
# String -----------------------------------
# Type Converting and Casting --------
print("Hello World")
print('Single Quotation')
# Implicit Type Conversion
# Scape character ------------------------ F1 = 12
print('It\'s fun to code \t with \t Python F2 = 12.0
\n') F3 = F1 + F2 # Implicit Type Conversion
print(type(F1), type(F2), type(F3))
# Multi-line Text ------------------------
# Explicit Type Conversion [Casting]
print(""" This is Multiline A1 = 12
Text in Python """) A2 = '45'
""" A3 = 36.7
This can be used as comments!
"""
B1 = float(A1)
B2 = str(12)
# String concatenation
print("Python " + "Language")
B3 = int(A2)
print(3 * 'Python ') B4 = int(A3) # Loss of data [ enforce
# print(5 + '4') # Wrong syntax casting ]
print(5 * '4')
print(B1, B2, B3, B4)
# Variable definition -----------------
A = 3 # -----------------------------------
print(A * 4) # print(int(input())*4)
A12 = 12
A_12 = 13 print("Python " + str(3.0))
_A = 14
# print(int(input("A:")) +
# Naming conventions ------------------ int(input("B:")))
# 1) camelCase
tempRotorInStart = 28.0 # In-place operators -----------------
# 2) PascalCase x = 10
TempRotorInStart = 30.0 x += 4
# 3) snake_case x -= 2
temp_rotor_in_start = 30.0 x /= 3
# 4) Pascal_Snake_Case x %= 3
Temp_Rotor_In_Start = 31.0 print(x)
# 5) CONSTANT_CASE s = "Python"
TEMP_ENV = 25.0 s += " Coding"
print(s)
# Python naming conventions -----------
# Programming idiom [Coding Style] ---

Python course By: Mansour Torabi


Variables and Operations

# the USUAL way to code in a specific


language
# In Python:
# Idiomatic Python code: being Pythonic
# - Unpythonic, a more pythonic solution

# (1) Swapping Variables


aa = 3
bb = 5
print(aa, bb)

aa, bb = bb, aa # Pythonic Style

print(aa, bb)

# (2) Line Continuation


BigString = "this is a long string or
statement that " \
" so I should split" \
" it to multiple lines"

# this is a fragile style, one space


after \ break the code

print(BigString)
# Pythonic Style
BigString2 = (
"This is "
"good "
"for me"
)

LongStatement = 2 + 3\
+ 4**4

LongStatement2 = ( # Pythonic Style


2 + 4/5 + 6
- 4 + 4*10
- 5 + 3 % 2 - 4**5
)
print(BigString2)
print(LongStatement)
print(LongStatement2)

# PEPs: Python Enhancement Proposals -

# PEP 8: Style Guide


# PEP 20: The Zen of Python
# ....

Python course By: Mansour Torabi

You might also like